#Local packages
79 messages · Page 1 of 1 (latest)
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
you do nothing, packages exists at the directory level
you can just use the functions in the other file
Yes, just make sure they declare the same package at the top.
Or the compiler will complain loudly.
just try it
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.
||you are also allowed to have a main_test package but that is just for the test suite and will not be compiled by go build but will by go test||
||Well, yeah, but that's not what is being discussed here :P||
||||that why it's in|| ||
||I figured as much. You're not worried we'll confuse Stackzz with this?||
||maybe but are the two || not good enough indicators ?||
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()
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
If both files are in the same directory you should have both of them as package main as per my message earlier
calc is in a diff directory
Then you need to import it, as per my message :P
whenever I press Code Runner
vscode removes the import
could it be a go.mod issue?
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
calc.brain(), except brain is lower case, so not exported. Rename it to Brain.
ah
Also, it's not required, I think, but packages are customarily named in lower case.
I renamed it to lowercase
.
├── 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()}
and put both source files in the same directory
then changed them both topackage main
Now this is the error
Do you have a go.mod?
yes
About this: When you supply go run with a specific file, it only compiles that singular file, not other files in the package
and now it works 🧐
Well you created it, so idk
Ok so if I want to test the code I have to do it that way from now on?
What you mean?
If I have multiple source files
and want to run the code
i need to type in go run .
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
Yeah should work