31

How to reference MyStruct in another file in the same package or folder?

Currently I get undefined: MyStruct when go build lib/file_2.go. I can run go install with no error, should I just ignore the build error?

These are my files:

lib/file_1.go

...
package lib
...
type MyStruct struct{
}
....

lib/file_2.go

...
package lib
...
{
m MyStruct
}
....

Thanks

1
  • Your current approach is it. It'll work. Commented Dec 18, 2014 at 14:45

5 Answers 5

19

This command works for me

go run *.go

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

Sign up to request clarification or add additional context in comments.

3 Comments

Don't do this. Use go build and go install, they exist for a reason. In particular using go run *.go will fail if you have *_test.go files (which you should have!) and it will silently ignore // +build constraints which will either cause it to fail or behave in an unexpected way depending on how the build tags are used.
Also, this question is talking about a file in a package other than main so go run has absolutely no bearing whatsoever and makes this irrelevant and not an answer.
Thanks Dave C for clearing everything. You are right. But this command works nice for small demo projects.
10

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.

2 Comments

Thanks and you can do "go build file_1.go file_2.go"
@ChrisG., you can but shouldn't. You should normally specify packages (or nothing for the package in the current directory) to go build (or go install or go test). Specifying files should be considered an advanced feature only used if you have some special need. Among other differences/issues, go build *.go ignores build constraints and uses a different way of determining the output executable name.
6

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.

Comments

2

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

go run .

Comments

0

You have to compile the multiple files using the build command first.

go build

Now you can see a build file got generated with the given name or the folder name in case of default. Then run the binary.

./binary_file_name

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.