#Local packages

79 messages · Page 1 of 1 (latest)

timid scroll
#

How do I use multiple source files in the same project.

tepid swan
#

What do you mean?

#

By importing them?

#

Need some clarification on the question

timid scroll
#

I mean like just calling other source file

#

Leys say I have my main.go (package main)

#

and in the same directory (For organization sake ) I create calculator.go

#

how would I use calculator.go functions in the main source file

errant forge
#

you do nothing, packages exists at the directory level

#

you can just use the functions in the other file

timid scroll
#

oh its that easy?

#

Its confusing because golang doesn't have classes

timber spear
#

Yes, just make sure they declare the same package at the top.

#

Or the compiler will complain loudly.

errant forge
timber spear
# timid scroll wdym

The file starts with package main, probably.
If the other file starts woth package notmain, or whatever, the compiler will complain that there are several different packages in the same directory.
So if you want them to be in different packages, they need to be in different directories.

Also, yeah, like YÇ said: Try it.

errant forge
timber spear
errant forge
timber spear
errant forge
#

||maybe but are the two || not good enough indicators ?||

tepid swan
#

You could either do

├── calculator.go // package main
└── main.go // package main

And then you just use the calc function in main.go without anything
Or you could do

.
├── calculator
│   └── calculator.go // package calculator
└── main.go // package main

and then in main.go you do

import "modulename/calculator"
calculator.Whatever()
timid scroll
#

Let me try this

#
package Calc

import "fmt"

func brain() { fmt.Println("Hola") }
#
func main() {
    brain()
}
``` ``(Main.go, package mai)``
#

It didnt work

#

command-line-arguments

.\main.go:108:2: undefined: brain

tepid swan
#

If both files are in the same directory you should have both of them as package main as per my message earlier

timid scroll
#

calc is in a diff directory

tepid swan
#

Then you need to import it, as per my message :P

timid scroll
#

whenever I press Code Runner

#

vscode removes the import

#

could it be a go.mod issue?

tepid swan
#

because you don't use it, you need to do calculator.Brain

#

On a side note, you need brain to be uppercase to be exported

timber spear
#

calc.brain(), except brain is lower case, so not exported. Rename it to Brain.

timid scroll
#

ah

timber spear
#

Also, it's not required, I think, but packages are customarily named in lower case.

timid scroll
#

I renamed it to lowercase

tepid swan
#
.
├── calc
│   └── calc.go
├── go.mod
└── main.go

go.mod:

module "foo"

calc.go:

package calc
func Brain() {}

main.go:

package main
import "foo/calc"
func main() {calc.Brain()}
timid scroll
#

and put both source files in the same directory

#

then changed them both topackage main

#

Now this is the error

tepid swan
#

Do you have a go.mod?

timid scroll
#

yes

tepid swan
#

Where are you on your computer?

#

like what directory

timid scroll
tepid swan
#

Ah i see

#

Try go run .

#

Well what's in tempCodeRunnerFile

timid scroll
#

I dont recall this being in there

#

literally only that is in there

tepid swan
#

???

#

What's in tempCodeRunnerFile.go

#

I'm confused

timid scroll
#

Me too

#

I deleted tempCodeRunnerFile.go

tepid swan
# tepid swan Try `go run .`

About this: When you supply go run with a specific file, it only compiles that singular file, not other files in the package

timid scroll
#

and now it works 🧐

tepid swan
#

Well you created it, so idk

timid scroll
tepid swan
#

What you mean?

timid scroll
#

If I have multiple source files

#

and want to run the code

#

i need to type in go run .

tepid swan
#

go run . compiles and runs the code in the current directory

#

If your code is somewhere else you would do go run path/to/dir

timid scroll
#

ah ok thanks

#

so if I wanted to go build instead

#

what would be the args?

tepid swan
#

Same

#

-o if you wanna change the exec

#

go help build

timid scroll
#

go build .

#

?

tepid swan
#

Yeah should work