I was playing and learning with the beego framework for go, but my hello world example didn't run and from what I determine is that beego does a chdir based on os.Args[0] and that the program is run there. And that is why the app runs, but can't find the views directory.
So backtracking to a simpler level, filename 'example.go':
package main
import (
"fmt"
"os"
)
func main() {
fmt.Println(os.Getwd())
fmt.Println(os.Args[0])
}
Will output:
/<directory 'go run this_file.go' was run in>/ <nil>
/tmp/go-build178877254/command-line-arguments/_obj/exe/example
I don't understand enough about go as why this the way it is. My first thought is that I'm using gvm (go version manager) and this is happening because something is off. Any help appreciated.
EDIT:
Apparently,
go build
./example
produces a better, expected, and successful outcome:
<directory './example' was run in>/ <nil>
<directory './example' was run in>/
go run
will compile (go build
) your source into the system TEMP
or TMP
environment variable, then execute the file from there. It's not really designed for the running of production code, as it obviously takes longer to compile and execute than to just execute.
The usual practice is to use go run
for development, and then go build
and distribute the binaries. go run
is really a convenience for the developer to quickly test code in one command. It does have some useful benefits in running portable source code between multiple, disparate systems, but, if you're going to deploy the code to all of these systems, it's usually normal to include the go build
in the deployment. Check out the full guide to the Go command
To make matters easier, it's quite possible to just cross-compile Go code to multiple target architectures on one machine, then just deploy the binaries to the relevant machines. This means that the target systems don't even need Go installed. In addition to the link above, you can check out goxc which provides an even simpler way, single line, cross-compilation tool.