I have a single file in the main
package called main.go
. Because the code isn't reusable I want to separate part of the code in a different file but in the same package.
How do I split the contents of main.go
into multiple files without creating a separate package?
I want a directory structure like this:
ls foo
# output:
main.go
bar.go
File bar.go
// file bar.go
package main
import "fmt"
func Bar() {
fmt.Println("Bar")
}
File main.go
// file main.go
package main
func main() {
Bar()
}
In main.go
i want to use func Bar()
from bar.go
. But when I run go run main.go
it results in:
# command-line-arguments
./main.go:4:2: undefined: Bar
What can be done do run the code without an error?
*_test.go
in your folder. Then you need to (1)shopt -s ext glob
and (2)go run !(*_test).go
. – Hirsutism