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
go run is just a shortcut for compiling then running in a single step. While it is useful for development you should generally build it and run the binary directly when using it in production. There must be more to it than that - if I time the println in a helloworld, it runs faster with go run than compiled.
From the command line in the hello directory, run the go build command to compile the code into an executable. From the command line in the hello directory, run the new hello executable to confirm that the code works.
go build builds the command and leaves the result in the current working directory. Above program will be able to turn into an executable file that will always print out “Hello World”. If we want our program to run again, we don't have to compile the program again, we simply run the executable file.
go build builds the command and leaves the result in the current working directory. go install builds the command in a temporary directory then moves it to $GOPATH/bin .
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With