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?

Nik
  • 2,885
  • 2
  • 25
  • 25
Dave
  • 151
  • 1
  • 1
  • 3
  • 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. – James Henstridge Jul 05 '14 at 02:41
  • 1. Use `go build` - go run is really only ideal for simple, single file programs. 2. Don't call the folder 'main'. – elithrar Jul 05 '14 at 06:00
  • Show your `$GOPATH` please – 0xAX Jul 05 '14 at 10:14
  • Possible duplicate of [golang "undefined" function declared in another file?](https://stackoverflow.com/questions/28153203/golang-undefined-function-declared-in-another-file) – Asalle Mar 07 '19 at 10:23

4 Answers4

16

Try

go run Test.go someFile.go
theaidem
  • 217
  • 1
  • 5
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

Community
  • 1
  • 1
warvariuc
  • 57,116
  • 41
  • 173
  • 227
  • @staticx: It provides an explanation of the error and a solution. It provides an answer to the question. – peterSO Jul 06 '14 at 13:42
  • 1
    Now it does. Your post showed up in the low quality queue. – Engineer2021 Jul 06 '14 at 13:43
  • 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! – Shadoninja Sep 26 '17 at 19:06
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 :)

Para
  • 3,681
  • 4
  • 23
  • 34
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)

NIMISHAN
  • 1,265
  • 4
  • 20
  • 29