Setting Your Mac for Go Development (GoLang)
In this post we are going to see how we can setup your Mac machine for development using GoLang. Go is an open source programming language from Google, which is very efficient for writing platform independent, highly scalable and concurrent code. Let’s first download Go. You can download the installer which should provide the necessary tools for development using the language.
The installer should also add go in your PATH. If it is successfully installed, you can verify it from the terminal. Just type go in your terminal.
In order to check which version of go is installed, you can use go version command in the terminal.
Setting $GOPATH
The Go installer just installs the necessary base for development but you need additional packages for developing a go application. They are separately downloaded. This requires GOPATH environment variable to be set. You can just create this bash_profile in your User folder.
We update .bash_profile as follows:
Setting VSCode for Go Development
In order to develop Go applications in VSCode, we have to set it up first. Ideally, it should pick up the system’s settings, but it doesn’t. We need to update settings to specify the folder used to download packages.
Let’s update the settings.json with the following settings:
For debugging support, we are not done yet with Visual Studio Code. As we try to hit Debug, we get the following message:
We can install this using Integrated Visual Studio Code terminal:
First Go App
Let’s write our first Go app. This is actually the skeleton of any Go App. This has a simple main function, which uses PrintLn function from fmt package to print a simple message to the console.
Go application can be built using go build command. It creates an executable in the same folder, which can be run directly. You can notice that the message written using fmt is printed on the console.