Home >Backend Development >Golang >How to install golang mac

How to install golang mac

PHPz
PHPzOriginal
2023-03-29 09:24:09714browse

Golang is a simple, fast, safe and efficient concurrency programming language. It is available on various operating systems, including macOS. In this article, we will discuss how to install Golang on macOS.

1. Download Golang
Before starting the installation, you need to download the latest version of Golang from https://golang.org/dl/. Golang installation files are usually less than 100MB in size, so the download speed should be fast.

2. Install Golang

  1. Open the terminal
    On macOS, you can open the terminal by finding it in the application. You can also use the shortcut "Command Space" to open Spotlight and then enter "Terminal" to search for it.
  2. Configure installation path
    Enter the following command in the terminal to create a new directory to store Golang binaries.

mkdir $HOME/go

Next, open the .bash_profile file with the following command:

open -e $HOME/.bash_profile

In the opened file, add the following content at the end:

export GOPATH=$HOME/go
export PATH=$PATH:$GOPATH/bin

  1. Install Golang
    Next, open the Golang installation file downloaded in the first step. This will unpack the Golang binaries into the "/usr/local/go" directory. However, in order for macOS to be able to find Golang's binaries, it needs to be added to the PATH environment variable. Complete the task through the following command:

echo 'export PATH=$PATH:/usr/local/go/bin' >> ~/.bash_profile

Next, restart the terminal .

  1. Check the installation
    After the installation is complete, you can check whether Golang is installed correctly by entering the following command in the terminal:

go version

If the command returns the currently running Golang version number, the installation is successful.

3. Run the first program
Next, we will create a simple "Hello, World!" program and run it. Enter the following command in the terminal:

mkdir $HOME/go/src/hello
cd $HOME/go/src/hello
echo 'package main

import "fmt "

func main() {

fmt.Println("Hello, World!")

}' > hello.go

Compile and run the program with the following command:

go run hello. go

Terminal output is as follows:

Hello, World!

This is the successful running of your first Golang program. Congratulations!

Summary
Golang is a very popular programming language, especially suitable for writing relatively small applications. Installing it on macOS is very simple and can be done easily even if you are a newbie. Just follow the steps provided in this article, and you can easily install Golang on the macOS platform and start writing your own programs.

The above is the detailed content of How to install golang mac. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn