Roel Van Nyen
Roel Van Nyen

Reputation: 1325

go build works fine but go run fails

I have a few files in the main package under one directory:

main.go config.go server.go

When I do: "go build" the program builds perfect and runs fine. When I do: "go run main.go" it fails.

Output:

# command-line-arguments
./main.go:7: undefined: Config
./main.go:8: undefined: Server

The symbols that are undefined are structs and they are capitalised so should be exported.

My Go version: go1.1.2 linux/amd64

Upvotes: 45

Views: 19383

Answers (3)

Piyush Bag
Piyush Bag

Reputation: 101

Good practise would be to create a package for it and run

go run ./app

Ex. Folder Structure

    ├──app/
    |  ├──main.go
    |  ├──config.go
    |  ├──server.go
    ├──bin/
    |  ├──foo.go
    └──pkg/
       └──bar.go

Upvotes: 3

Daniel Pecos
Daniel Pecos

Reputation: 581

You could execute it as:

go run .

so you don't have to include all files manually.

Upvotes: 43

Nick Craig-Wood
Nick Craig-Wood

Reputation: 54081

This should work

go run main.go config.go server.go

Go run takes a file or files and it complies those and only those files which explains the missing symbols in the original post.

Upvotes: 67

Related Questions