I wanted to try out Go and the first step was to install it. Unlike other languages I've used, say python for example, I had an extremely hard time installing it. Installing Go itself was simple, but when I'd go to use packages, things just didn't see to work as I expected. In a few different cases I ended up uninstalling Go and giving up. But I really wanted to try Go, so I did some research and from a few different articles, I was able to piece a process together that worked perfectly on both my Ubuntu 20.04 laptop and my M1 MacBook Pro. For this article, I am going to be installing the latest version of Go on my WSL 2 Ubuntu 22.04 distro.
curl -OL https://go.dev/dl/go1.19.1.linux-amd64.tar.gz
Optional but encouraged, verify the integrity of the file you downloaded by comparing the checksum to the one on the Go releases page:
sha246sum https://go.dev/dl/go1.19.1.linux-amd64.tar.gz
# output
acc512fbab4f716a8f97a8b3fbaa9ddd39606a28be6c2515ef7c6c6311acffde go1.19.1.linux-amd64.tar.gz

/usr/local)sudo tar -C /usr/local -xvf go1.19.1.linux-amd64.tar.gz
# Open up your .bashrc, .zshrc, or .profile
nano ~/.zshrc
# Add Go's root value to your path by adding this info to the end of the file
export PATH=$PATH:/usr/local/go/bin
# refresh from session
source ~/.zshrc
go version
# output
go version go1.19.1 linux/amd64
# create workspace directory
mkdir ~/go
# Open up your .bashrc, .zshrc, or .profile
nano ~/.zshrc
# Add Go's root value to your path by adding this info to the end of the file
export GOPATH=$HOME/go
# refresh from session
source ~/.zshrc
srccd go/
mkdir src
cd src/
src folder create your first project; we'll call ours hellomkdir hello
cd hello/
hello directory create a module file for managing dependencies by running
_Note: for 'yourdomain' I used my github usernamego mod init your_domain/hello
After running this you will see you now have a go.mod file in your folder 6) Next create a simple hello world application
nano hello.go
#contents
package main
import "fmt"
func main() {
fmt.Println("Hello, World!")
}
go run hello.go
# output
Hello, World!
The beauty of Go, in my opinion, is the ease at which you can create a binary executable and enable your program so that it can run from anywhere on your system. Here is how we do this.
hello directory compile your program into an executable binarygo build
./hello
# output
Hello, World!
You've successfully turned your code into an executable binary. However, your code can only be run from this directory. You could run the program from another location by passing the full path to the executable but that could quickly become annoying. Instead, Go provide us with a way to install the program so that it can be run from anywhere. 3) From within the hello folder Install your program by running
go install
This creates a bin folder within our $GOPATH and installs the binary executable there. 4) Add the location of this folder to your $PATH
# Open up your .bashrc, .zshrc, or .profile
nano ~/.zshrc
# Add Go's root value to your path by adding this info to the end of the file
export PATH=$PATH:$HOME/go/bin/
# refresh from session
source ~/.zshrc
cd $HOME
hello
# output
Hello, World
Hopefully, everything worked and you got the expected output. You've now successfully installed Go. Happy coding!
If you want to double check that go is installed correctly then try these additional steps.
cd /go/src/
mkdir newProject
go.mod filego mod init github.com/git_username/newProject
Now, let's make sure we can install and run a library. In this case, I'm going to install a library for making modern CLI applications called Cobra. Prior to finding the "correct" install method, I would install the library, attempt to initialize my project with it, but nothing would happen. Here's what should happen. 3) First, install the library from within our project folder
go install github.com/spf13/cobra-cli@latest
The library may take a moment to install. 4) Cobra has its own CLI, verify it was installed correctly by running
cobra-cli
# output
Cobra is a CLI library for Go that empowers applications.
This application is a tool to generate the needed files
to quickly create a Cobra application.
Usage:
cobra-cli [command]
...
If Go is installed correctly then you should see output from Cobra explaining how to use it. This is the part that not working for me before. I would run cobra-cli and nothing would happen. Once I figured out how to properly install and configure Go, then everything worked as it should.
cobra-cli init
Now you can build your own super awesome CLI tool.
tree -L 1
# output
.
├── bin
├── pkg
└── src
I hope you found something in this post helpful and that you have a blast coding in Go.