Best Way To Download Golang On Mac

Refactored and improved a medium-sized Golang legacy project using VSCode as IDE. Had trouble configuring and integrating a debugger, but works great after it is done. Code navigation for Go (with plugins) is sometimes not powerful enough: no way to fuzzy-search-and-jump to a method by 'class' and method name.

Go is an open source, statically typed, compiled programming language built by Google.

It combines the best of both statically typed and dynamically typed languages and gives you the right mixture of efficiency and ease of programming. It is primarily suited for building fast, efficient, and reliable server side applications.

Following are some of the most noted features of Go -

  • Safety : Both Type safety and Memory safety.
  • Good support for Concurrency and communication.
  • Efficient and latency-free Garbage Collection
  • High speed compilation
  • Excellent Tooling support

This is the first part of our tutorial series on Go. In this article, you’ll learn how to install Go in your system and set up your development environment for Go projects.

Installing Go

Go binary distributions are available for all major operating systems like Linux, Windows, and MacOS. It’s super simple to install Go from the binary distributions.

If a binary distribution is not available for your operating system, you can try installing Go from source.

Mac OS X

Using Homebrew

The easiest way to install Go in Mac OS is by using Homebrew -

Using macOS package installer

Download the latest Go package (.pkg) file from Go’s official downloads page. Open the package and follow the on-screen instructions to install Go. By default, Go will be installed in /usr/local/go.

Linux

Download the Linux distribution from Go’s official download page and extract it into /usr/local directory.

Next, add the /usr/local/go/bin directory to your PATH environment variable. You can do this by adding the following line to your ~/.bash_profile file -

You can also use any other directory like /opt/go instead of /usr/local for installing Go.

Windows

Download the Windows MSI installer file from Go’s official download page. Open the installer and follow the on-screen instructions to install Go in your windows system. By default, the installer installs Go in C:Go

The Go tool

The Go distribution comes bundled with the go tool. It is a command line tool that lets you automate common tasks such as downloading and installing dependencies, building and testing your code, and much more.

After installing Go by following the instructions in the previous section, you should be able to run the Go tool by typing go in the command line -

Golang

GOPATH, Go Workspace, and Go Code Organization

Go requires you to organize your code in a specific way -

By convention, all your Go code and the code you import, must reside in a single workspace. A workspace is nothing but a directory in your file system whose path is stored in the environment variable GOPATH.

Note that, after the introduction of Go modules in Go 1.11, you’re no longer required to store your Go code in the Go workspace. You can create your Go project in any directory outside of GOPATH. The following explanation of Go workspace is given for historical reasons and the fact that it’s still valid. You can skip this section if you want.

The workspace directory contains the following sub directories at its root -

  • src: contains Go source files.

    The src directory typically contains many version control repositories containing one or more Go packages. Every Go source file belongs to a package. You generally create a new subdirectory inside your repository for every separate Go package.

  • bin: contains the binary executables.

    The Go tool builds and installs binary executables to this directory. All Go programs that are meant to be executables must contain a source file with a special package called main and define the entry point of the program in a special function called main().

  • pkg: contains Go package archives (.a).

    All the non-executable packages (shared libraries) are stored in this directory. You cannot run these packages directly as they are not binary files. They are typically imported and used inside other executable packages.

Setting GOPATH

The GOPATH environment variable specifies the location of your workspace. By default, the GOPATH is assumed to be $HOME/go on Unix systems and %USERPROFILE%go on Windows. If you’re happy with this path then you don’t need to do anything. You can just create your workspace directory named go inside the home folder and start writing Go code.

Best Way To Download Golang On Mac Download

If you want to use a custom location as your workspace, you can set the GOPATH environment variable by following the instructions below -

Unix Systems (Linux and macOS)

For setting GOPATH in bash shells, add the following line to the ~/.bash_profile file -

If you use Zsh shell, then you need to add the above line to ~/.zshrc file.

Windows System

Let’s say that you want to have your workspace directory at C:go-workspace. Here is how you can set the GOPATH environment variable to use this workspace location -

  • Create the workspace folder at C:go-workspace.

  • Right click on Start → click Control Panel → Select System and Security → click on System.

  • From the menu on the left, select the Advanced systems settings.

  • Click the Environment Variables button at the bottom.

  • Click New from the User variables section.

  • Type GOPATH into the Variable name field.

  • Type C:go-workspace into the Variable value field.

  • Click OK.

Note that, GOPATH must be different than the path of your Go installation.

Testing your Go installation with the Hello World program

First, make sure that you have created the Go workspace directory at $HOME/go. Next, create a new directory src/hello inside your workspace. Finally, create a file named hello.go with the following code -

Note: after Go 1.11 onwards, you can also create and run the program outside of Go workspace.

The easiest way to run the above program is using the go run command -

Building an executable binary using go build

The go run command compiles and runs your program at one go. If however, you want to produce a binary from your Go source that can be run as a standalone executable without using the Go tool, then use the go build command -

The go build command creates an executable binary with the same name as the name of your immediate package (hello). You can run the binary file like so -

Installing the package into the bin directory using go install

You can use the go install command to build and install the executable binary into your workspace’s bin directory -

You can also add the $HOME/go/bin directory to the PATH variable to run go executables from any location.

Don’t forget to check out: go help run, go help build, go help install.

Best Way To Download Golang On Mac High Sierra

Conclusion

That’s all for now folks! I hope you’re all set to deep dive and learn more about the Go programming language. You can find all the tutorials written in Go from the Golang categories page.

Thanks for reading, see you in the next article.

Next Article: Hello Golang: Writing your first Golang Program

Code Samples: github.com/callicoder/golang-tutorials

More resources