AppDividend
Latest Code Tutorials

How To Install Golang On Mac Example From Scratch

0

Install Golang on Mac is very easy. You can download the binary from the official website or install it using homebrew. Golang provides binary distributions for Mac OS X, Windows, and Linux. Then set up your Golang environment. If you are using a different OS, you can download the Go source code and install it from the source.

How To Install Golang On Mac

  1. Download the latest version of Go for your platform here: https://golang.org/dl/.
  2. Follow the instructions for your platform to install the Go tools: https://golang.org/doc/install#install. It is recommended to use the default installation settings.
  3. On Mac OS X and Linux, by default Go is installed to directory /usr/local/go, and the GOROOT environment variable is set to /usr/local/go/bin.
  4. You may need to restart any open Terminal sessions for the change to take effect.
  5. Also, we need to install the VSCode, because there are some tools which we will install inside VSCode which will help us during the developing Go project.

Now, Create a go file named hello.go and add the following code.

/* hello.go */

package main

import "fmt"

func main() {
   fmt.Printf("Welcome to GO AppDividend")
}

Okay, now you can run the file using two ways.

First, you can compile the file using the following command.

go build

It will create an executable file with the same file name. So, if your filename is hello.go, then it will create a hello file. On MAC, it is just hello file, in windows, it is a hello.exe file.

Now, run the executable file on Mac using the following command.

./hello

It will give the output in the terminal.

How To Install Go On Mac

There is also another way in which you get the direct output.

Type the following command in your terminal.

go run hello.go

Go Programming Language

In Visual Studio Code, our project looks like below.

How To Install Go On Mac | Go Programming Language

Install Golang Tools On Mac

If you are upgrading from an older version of Go you must first remove the existing version.

Linux, macOS, and FreeBSD tarballs

Download the archive and extract it into /usr/local, creating a Go tree in /usr/local/go. For example:

tar -C /usr/local -xzf go$VERSION.$OS-$ARCH.tar.gz

Choose the archive file appropriate for your installation. For instance, if you are installing Go version 1.2.1 for 64-bit x86 on Linux, the archive you want is called go1.2.1.linux-amd64.tar.gz.

(Typically these commands must be run as root or through sudo.)

Add /usr/local/go/bin to the PATH environment variable. You can do this by adding this line to your /etc/profile (for a system-wide installation) or $HOME/.profile:

export PATH=$PATH:/usr/local/go/bin

Note: Changes made to a profile file may not apply until the next time you log into your computer. To apply the changes immediately, just run the shell commands directly or execute them from the profile using a command such as source $HOME/.profile.

Finally, How To Install Go On Mac | Go Programming Language Tutorial is over.

Recommended Posts

Go Functions example

Golang Receiver function example

Go Interface example

Go Arrays example

Go Structs example

Leave A Reply

Your email address will not be published.

This site uses Akismet to reduce spam. Learn how your comment data is processed.