Introduction
Go, sometimes referred to as “Golang”, is an open-source programming language that was released by Google in 2012. Google’s intention was to create a programming language that could be learned quickly.
Hello, World!
Prerequisites
sudo
Step 1 — Installing Go
In this step, you will install Go on your server.
ssh
- ssh sammy@your_server_ip
Next, navigate to the official Go downloads page in your web browser. From there, copy the URL for the current binary release’s tarball.
linux-amd64.tar.gz
Now that you have your link ready, first confirm that you’re in the home directory:
- cd ~
curl-OL
- curl -OL https://golang.org/dl/go1.16.7.linux-amd64.tar.gz
sha256sum
- sha256sum go1.16.7.linux-amd64.tar.gz
This will return the tarball’s SHA256 checksum:
Outputgo1.16.7.linux-amd64.tar.gz
7fe7a73f55ba3e2285da36f8b085e5c0159e9564ef5f63ee0ed6b818ade8ef04 go1.16.7.linux-amd64.tar.gz
If the checksum matches the one listed on the downloads page, you’ve done this step correctly.
tar-C/usr/local/xtarvf
- sudo tar -C /usr/local -xvf go1.16.7.linux-amd64.tar.gz
/usr/local/go
Step 2 — Setting Go Paths
In this step, you will set paths in your environment.
.profile
.profilenano
- sudo nano ~/.profile
Then, add the following information to the end of your file:
. . .
export PATH=$PATH:/usr/local/go/bin
nanoCTRL+XYENTER
Next, refresh your profile by running the following command:
- source ~/.profile
gogo version
- go version
This command will output the release number of whatever version of Go is installed on your system:
Outputgo version go1.16.7 linux/amd64
This output confirms that you are now running Go on your server.
Step 3 — Testing Your Install
Hello, World!
First, create a new directory for your Go workspace, which is where Go will build its files:
- mkdir hello
Then move into the directory you just created:
- cd hello
go.modgo mod init
- go mod init your_domain/hello
Hello, World!
- nano hello.go
hello.go
package main
import "fmt"
func main() {
fmt.Println("Hello, World!")
}
CTRL+XYENTER
Hello, World!
- go run .
OutputHello, World!
go run.gohellogo build
Step 4 — Turning Your Go Code Into a Binary Executable
go rungo buildgo installHello, World!
go buildhello.go
- go build
./hello
- ./hello
OutputHello, World!
hello.go
go installgo buildgo install$GOPATH/bin
go installgo buildgo list
- go list -f ‘{{.Target}}’
go listfgo listTargetgo list
Output‘/home/sammy/go/bin/hello
go build/home/sammy/go/bin/
Add this install directory to your system’s shell path. Be sure to change the highlighted portion of this command to reflect the install directory of the binary on your system, if different:
- export PATH=$PATH:/home/sammy/go/bin/
go install
- go install
hello
- hello
OutputHello, World!
Hello, World!
Conclusion
By downloading and installing the latest Go package and setting its paths, you now have a system to use for Go development. You can find and subscribe to additional articles on installing and using Go within our “Go” tag