15

I have 2 go files:

/Users/username/go/src/Test/src/main/Test.go

package main

import "fmt"

func main() {
    fmt.Printf(SomeVar)
}

and file /Users/username/go/src/Test/src/main/someFile.go

package main

const SomeVar = "someFile"

However I am constantly getting compiler error:

/Users/username/go/src/Test/src/main/Test.go:6: undefined: SomeVar

Can someone explain to me why is SomeVar labeled as undefined?

4
  • 2
    How are you invoking the compiler? If you ran go build Test.go, then it will only consider that one file to be part of the package. Commented Jul 5, 2014 at 2:41
  • 1. Use go build - go run is really only ideal for simple, single file programs. 2. Don't call the folder 'main'. Commented Jul 5, 2014 at 6:00
  • Show your $GOPATH please Commented Jul 5, 2014 at 10:14
  • Possible duplicate of golang "undefined" function declared in another file? Commented Mar 7, 2019 at 10:23

4 Answers 4

16

Try

go run Test.go someFile.go
Sign up to request clarification or add additional context in comments.

Comments

11

Quote:

I think you're misunderstanding how the go tool works. You can do "go build" in a directory, and it'll build the whole package (a package is defined as all .go files in a directory). Same for go install, go test, etc. Go run is the only one that requires you to specify specific files... it's really only meant to be used on very small programs, which generally only need a single file.

So do:

go build && ./program_name

See also

3 Comments

@staticx: It provides an explanation of the error and a solution. It provides an answer to the question.
Now it does. Your post showed up in the low quality queue.
This helped me! I was running into this issue trying to run my large program in Gogland. I fixed it by going to the run configurations and switching the project type to directory!
9

You code is correct:

  • someFile.go and Test.go belong to the same package (main)
  • SomeVar is a const declared at top level, so it has a package block scope, namely the main package block scope
  • as a consequence, SomeVar is visible and can be accessed in both files

(if you need to review scoping in Go, please refer to the Language Specification - Declarations and Scope).

Why do you get an undefined error then?

You probably launched go build Test.go or go run Test.go, both producing the following output, if launched from /Users/username/go/src/Test/src/main:

# command-line-arguments
./Test.go:6: undefined: SomeVar

You can find the reason here: Command go

If you launch go build or go run with a list of .go files, it treats them as a list of source files specifying a single package, i.e., it thinks there are no other pieces of code in the main package, hence the error. The solution is including all the required .go files:

go build Test.go someFile.go

go run Test.go someFile.go

go build will also work with no arguments, building all the files it finds in the package as a result:

go build

Note 1: the above commands refer to the local package and, as such, must be launched from the /Users/username/go/src/Test/src/main directory

Note 2: though other answers already proposed valid solutions, I decided to add a few more details here to help the community, since this is a common question when you start working with Go :)

Comments

2

Due to landing on this page when I had a similar question, I'll include this answer alongside of what has already been stated. If you are using go run you may use:

go run . or go run .*

This question was also answered on this question

How to run all .go files within current directory through the command line (multi file package)

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.