Introduction
As the end of the year is fast approaching, I feel like a lot of changes are also happening. First, I ended up adopting a red heeler mix puppy. He is about 8 months old and super active – which is great because I am training for an ultramarathon and need a training partner. He is super smart and already knows quite a few tricks. He is still quite shy and when he is scared or nervous, he likes to sit on my shoes. I’m not sure what other breed he may be, but I’m open to guesses. His current name is Buster and I kind of feel like I can’t change it because he already knows it. Buster is basically my boss now and all my time revolves on what he wants to do.
Secondly, I was recently promoted and then simultaneously accepted a new position as a Site Reliability Engineer. I am very excited about this position because it will be a great opportunity for me to learn more about Kubernetes, OpenShift and Go. I literally know nothing about Go so I need to really put my head down and learn it in the next couple of weeks. I hear Go is similar to C, but I haven’t done anything with C for many years.
First things first though, installing Go on my Mac mini.
Procedure
Step 1. Head on over to go.dev.
Step 2. Click on Download:
Step 3. Click on binary for your system. I have a Mac mini with the M1 chip so I have selected the Darwin binary.
Step 4. Once the package is downloaded, click on it to start the installer:
Step 5. Click on Continue and accept the defaults to install Go:
Step 6. Open up a terminal and type go version to confirm Go is installed and in your path:
underacloud:~$ go version
go version go1.19.4 darwin/arm64
NOTE: If you want to change your default shell, you can run the command chsh -s /bin/bash
.
If you are using Visual Studio Code (VS Code) as your editor, you can install the Go extension in VS Code.
Step 1. Open up VS Code and click on the Extensions button:
Step 2. Type Go into the search box and and then click on Install to install the Go extension:
Step 3. Create a new file and then click on the Select a language link and type Go. When prompted, click Install All at the bottom of the screen. The installation process may take a bit of time because there are a lot of tools to download and install:
Once complete, you should receive a message that says: All tools successfully installed. You are ready to Go. :)
Let’s create the typical “Hello World” program in Go.
Step 1. Add the following to your file:
package main
import "fmt"
func main() {
fmt.Println("hello world")
}
Step 2. Save the file as hello-world.go
Step 3. In a terminal, type go run hello-world.go
underacloud go % go run hello-world.go
hello world
Step 4. To build your program into a binary, type
go build hello-world.go
Step 5. Type ls and you should see the hello-world binary. You can run it by typing ./hello-world:
underacloud go % ls
hello-world hello-world.go pkg
underacloud go % ./hello-world
hello world
Conclusion
Today we learned how to install Go on MacOS and also how to write our first program in Go.