Learning Golang: Compiling and running Go programs
This is part 2 of my journey learning Golang.
Compiling Go programs
An individual Go source file can be compiled with the go build
command:
1go build {filename}.go
That will produce an executable binary file if the source’s package is main
(see Part 3 for more on packages).
The resulting binary can be executed with this command:
1./{filename}
Example:
1$ go build main.go
2$ ./main
3Hello World
Running from source
The go run
command combines the two previous steps: it builds a binary from a source file and executes it:
1go run {filename}.go
That’s super handy for an interactive workflow during development.
Example:
1$ go run main.go
2Hello World
go run
does not create a binary in the current folder.
Takeaways
go build
will compile and produce a binary.go run
will compile and run without producing a binary.
This looks like a pretty handy workflow to me. What do you think? How does that compare to your experience with other languages? If you’re a seasoned Go programmer, what would you add to these notes?
comments powered by Disqus