golang reference struct in same package in another file

You're asking the go tool to compile lib/file_1.go, you never mention lib/file_2.go so how is it supposed to know it should compile it?

From go help build:

Build compiles the packages named by the import paths,
along with their dependencies, but it does not install the results.

If the arguments are a list of .go files, build treats them as a list
of source files specifying a single package.

You should be able to use MyStruct directly, since it is in the same package as its definition.

If you have any issue, sometimes it can help (for an IDE like SublimeText + GoSublime, for instance) to do a go install before creating lib/file_2.go.
That way, lib/file_1.go is compiled and present in GOPATH/pkg, with lib/file_1.go definitions visible for lib/file_2.go to use during its compilation.


This command works for me

go run *.go

Actually this will compile all go file and run your main function. So this works well


According to https://golang.org/doc/tutorial/getting-started, you should run:

go run .

Tags:

Go