Get Going With Go
Derek D.
Posted on April 15, 2020
This article is meant to be a quick introduction to installing the Go programming language, running your first script, and building a binary that can be executed on Windows, Mac OS X, and Linux. I've included links to resources from Google and Digital Ocean that go into much greater detail on these topics but if you are just looking for a quick 5-minute introduction this is the article for you.
Downloading Go
Google provides an installer for the major operating systems (Windows, macOS X, and Linux). You can also download a tarball of binaries and install directly from source code, but that's out of scope for this article. For this step download and run the installer for your OS, following the prompts.
You've probably heard or seen articles about the GOPATH before. This was a top-level directory where all of your go projects had to be located. Fortunately, it's no longer needed. At least not on Mac OS X. At this point, a little code is needed to test the installation.
Go Say Hello
Unfortunately, Go doesn't have a REPL (Read Evaluate Print Loop) so you'll be saving and running code from a file.
Copy the code below and save it as greeting.go
. Golang requires the .go
extension but the rest of the filename could be anything you want. Without covering Go syntax to much this simple script when executed, prompts for a name and says hello to whatever name was entered.
package main
import (
"bufio"
"fmt"
"os"
)
func main() {
reader := bufio.NewReader(os.Stdin)
fmt.Print("What is your name? ")
text, _ := reader.ReadString('\n')
fmt.Println("Hello ", text)
}
There are 2 important structural parts to the sample code. The first is package main
which identifies this file contains the entrypoint to your code. The second is func main()
which is the actual entrypoint. If either of these is missing or is not named main the code will not execute.
Go Run V Go Build
There are 2 ways to run the sample script. During development, you'll mostly use go run
which compiles the code and executes it in one step. When it comes time to deploy the code you should use go build
which creates a binary that can be executed by calling directly from the terminal.
Go Run
Go Run is pretty simple and has one required argument, a package name. In this case, you will use the filename. Here is an example of the greeting.go
package. go run greeting.go
.
Go Build
Go build is a little more complex. The simplest form is go build <package-name>
. This creates a binary named the same as the package name, minus the .go
extension. So go build greeting.go
would create a binary named greeting
. The new binary can be executed with ./greeting
.
The more advanced form is using the -o
flag which allows you to name the binary produced by go build
. Rebuilding the greeting.go
package using the -o
flag would look like this go build -o hello.exe greeting.go
which would produce a binary file named hello.exe
.
Conclusion
- Use
go run <package-name>
when testing code in development. - Use
go build <package-name>
when preparing to ship code. - The file with
package main
at the top of the file, must contain thefunc main()
function which is the entrypoint to the app. - Your Go code no longer has to go under the GOPATH.
Additional Resources
How To Build And Install Go Programs by Digital Ocean
Posted on April 15, 2020
Join Our Newsletter. No Spam, Only the good stuff.
Sign up to receive the latest update from our blog.
Related
November 29, 2024