12

Following is how the source files are organized

✘-1 ~/Go/src/github.com/krmahadevan/packages 
18:24 $ tree .
.
├── sample_main.go
└── sample_one.go

0 directories, 2 files

Here's how the source code looks like:

sample_one.go

package main

var data map[string]string

func init() {
    data = make(map[string]string, 0)
}

sample_main.go

package main

import "fmt"

func main() {
    data["foo"] = "bar"
    fmt.Println(data)
}

Now when I attempt at running sample_main.go I get errors stating that data is undefined.

18:24 $ go run sample_main.go 
# command-line-arguments
./sample_main.go:6:2: undefined: data
./sample_main.go:7:14: undefined: data
✘-2 ~/Go/src/github.com/krmahadevan/packages

But when I build the code into a binary and then execute it, it runs fine.

✔ ~/Go/src/github.com/krmahadevan/packages 
18:27 $ go build
✔ ~/Go/src/github.com/krmahadevan/packages 
18:28 $ ./packages 
map[foo:bar]
✔ ~/Go/src/github.com/krmahadevan/packages

I would like to understand why is this behavior ?

Environment:

18:31 $ go version
go version go1.11.4 darwin/amd64

The closest I found was this post : Golang : command-line-arguments undefined: variable

But this post talks about scoped variables that are defined in main.

But my problem statement involves variables defined in another go file and accessed in the main method.

1
  • Sometimes there are multiple entry points and for all of them to run we need to run them all. In Go (Unix-Based Terminal): go run . should suffice. Commented Mar 24, 2023 at 9:57

2 Answers 2

23

To understand why, read the go command documentation:

Command go

Compile and run Go program

Usage:

go run [build flags] [-exec xprog] package [arguments...]

Run compiles and runs the named main Go package. Typically the package is specified as a list of .go source files, but it may also be an import path, file system path, or pattern matching a single known package, as in 'go run .' or 'go run my/cmd'.

Compile packages and dependencies

Usage:

go build [-o output] [-i] [build flags] [packages]

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

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

For more about specifying packages, see 'go help packages'. For more about where packages and binaries are installed, run 'go help gopath'.


go run: Typically the package is specified as a list of .go source files.

For your go run example, list the files:

go run sample_main.go sample_one.go
Sign up to request clarification or add additional context in comments.

2 Comments

thanks for helping out with this question. That solved the problem. Just to add a bit more context, in case anyone is facing this issue when running from within an IDE such as GoLang IDE, one needs to edit configuration and choose Run kind as Package.
@KrishnanMahadevan - unrelated comment. could you please help me in this testng question ? stackoverflow.com/questions/60196503/… thank you.
5

To fix it, just run the following command:

Windows: go run * or go build *

Unix: go run . or go build .

You are going to run it inside the folder of the files you want to use.

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.