Go, also known as Golang, is a powerful and modern programming language developed by Google. It is widely used for its simplicity, speed, and advanced concurrency features. In this article, we'll guide you through setting up and running Go on a macOS system with examples ranging from basic to advanced.
Installing Go
Follow these steps to download and install Go on your macOS system.
1. Download Go
- Visit the official Go download page.
- Download the macOS package (ending in .pkg file).
2. Install Go
Locate the downloaded package in your Downloads folder, then:
- Double click on the
go.pkgfile. - Follow the on-screen instructions to complete the installation.
3. Verify Installation
To confirm that Go is installed correctly, open your terminal and type:
go versionYou should see output such as go version go darwin/amd64, which confirms that Go is installed.
Setting Up Your Go Workspace
A Go workspace is a directory hierarchy with three directories at its root: src, pkg, and bin. Here is how you set it up:
mkdir -p ~/go/{bin,pkg,src}Then set the GOPATH environment variable to point to your Go workspace:
export GOPATH="$HOME/go"Add this line to your shell profile so it's set every time you open a terminal:
echo 'export GOPATH="$HOME/go"' >> ~/.bash_profileReload your profile:
source ~/.bash_profileCreating Your First Go Program
Basic Example
Let's write a simple "Hello, World!" program to test the installation:
package main
import "fmt"
func main() {
fmt.Println("Hello, World!")
}Save this code in a file called hello.go inside your src directory:
nano ~/go/src/hello.goRunning the Program
To run your Go program, navigate to the src directory and execute the following command:
go run hello.goYou should see the output:
Hello, World!Intermediate Example: Building a Simple CLI Tool
Now that you've run a basic program, let's build a simple Command Line Interface (CLI) tool:
package main
import (
"bufio"
"fmt"
"os"
)
func main() {
reader := bufio.NewReader(os.Stdin)
fmt.Print("Enter text: ")
text, _ := reader.ReadString('\n')
fmt.Println("You entered: ", text)
}Save this file as cli-tool.go and run it:
go run cli-tool.goIt will prompt you to enter text, which it then outputs back to you.
Advanced Example: Concurrency with Goroutines
Go's most exciting feature is its built-in support for concurrent programming. Here's a quick example using goroutines:
package main
import (
"fmt"
"time"
)
func main() {
go printNumbers() // start a goroutine
printLetters()
}
func printNumbers() {
for i := 1; i <= 5; i++ {
fmt.Printf("%d ", i)
time.Sleep(500 * time.Millisecond)
}
}
func printLetters() {
for i := 'a'; i <= 'e'; i++ {
fmt.Printf("%c ", i)
time.Sleep(1000 * time.Millisecond)
}
}Save this as concurrent.go and run it:
go run concurrent.goThis program demonstrates basic concurrency, with two functions running simultaneously. The numbers will be printed concurrently with letters, showcasing Go's concurrency model.
Conclusion
You've set up a Go programming environment on macOS, created a simple Go workspace, and run basic to advanced Go programs. Exploring and experimenting further with Go modules and libraries will deepen your understanding of this powerful language.