Golang Tutorial

Fundamentals

Control Statements

Functions & Methods

Structure

Arrays & Slices

String

Pointers

Interfaces

Concurrency

Installing Golang on MacOS

Let's walk through the process of installing Go (often called Golang) on MacOS.

1. Download the Installer:

  • Go to the official Go downloads page: https://golang.org/dl/.

  • Choose the distribution appropriate for MacOS (it should have a name like goX.X.X.darwin-amd64.pkg, where X.X.X represents the version number).

2. Install Go:

  • Open the downloaded .pkg file. This will start the installation process.

  • Follow the prompts to install Go. The installer will set up the appropriate directories and paths.

3. Verify the Installation:

Once the installation is complete:

  • Open your terminal.

  • Enter go version to check if Go is installed correctly. It should display the version of Go that you've installed.

$ go version
go version goX.X.X darwin/amd64
  • Additionally, you can check the Go environment setup using:
$ go env

4. Set up Your Workspace (Optional but Recommended):

Go has a unique approach to organizing code that's slightly different from other languages. While recent versions of Go have lessened the strictness of the workspace structure, it's still a good practice, especially if you're starting.

  • Go code is kept inside a workspace. A workspace is a directory in your filesystem whose path is stored in the environment variable GOPATH.

  • By default, GOPATH is set to ~/go. So, you'll have a directory in your home directory called go (i.e., /Users/your-username/go), which will be your workspace.

  • Inside this go directory, there are typically three subdirectories: src (where your Go source code lives), bin (where the compiled binaries live), and pkg (where compiled package files live).

  • To start a new project, create a new directory inside the src directory, and start writing your Go code in there.

5. Install Additional Tools (Optional):

Go provides a tool called go get which can be used to fetch and install Go packages and commands. This can be useful for installing additional tools and libraries.

For example, to install the popular Go code formatter gofmt, you'd use:

$ go get -u golang.org/x/tools/cmd/gofmt

Conclusion:

Now you've successfully installed Go on your MacOS and set up a basic workspace. The next steps would be to familiarize yourself with the language syntax, best practices, and other tooling that can help you become more productive with Go.