Home >Backend Development >Golang >Steps to set up and install Golang development environment on Mac computer

Steps to set up and install Golang development environment on Mac computer

WBOY
WBOYOriginal
2024-02-24 16:30:17791browse

Mac电脑上如何配置和安装Golang 开发环境?

Mac computers are the favorite working platform of many developers, and Golang, as an efficient programming language, is also loved by more and more people. This article will introduce in detail how to configure and install the Golang development environment on a Mac computer, and provide specific code examples to help readers quickly get started and develop using Golang.

Step 1: Download the Golang installation package

First, we need to download it from the Golang official website (https://golang.org/dl/) for Mac OS Golang installation package. Select the latest stable version and save the installation package to the local directory after the download is complete.

Step 2: Install Golang

  1. Open the Terminal application, enter the download directory, and use the following command to decompress the downloaded Golang installation package:
tar -C /usr/local -xzf go1.17.1.darwin-amd64.tar.gz
  1. Next, set the Golang environment variables. Edit the ~/.bash_profile file (if it does not exist, create the file) and add the following content:
export PATH=$PATH:/usr/local/go/bin
export GOPATH=$HOME/go
  1. After saving the file, execute the following command to make the configuration effective :
source ~/.bash_profile
  1. Finally, verify whether Golang is installed successfully and execute the following command:
go version

If the installation is successful, the currently installed Golang version information will be displayed.

Step 3: Create and run the first Golang program

Next, we will create a simple Hello World program and run it.

  1. First, create a new Golang working directory and enter the directory:
mkdir ~/go/src/hello
cd ~/go/src/hello
  1. In the working directory, create a file named hello .go file and open it with a text editor:
touch hello.go
  1. Edit the hello.go file and enter the following code:
package main

import "fmt"

func main() {
    fmt.Println("Hello, Golang!")
}
  1. After saving the file, return to the terminal and use the following command to run the Hello World program:
go run hello.go

If everything goes well, the terminal will output the message "Hello, Golang!", indicating the program. Run successfully.

Through the above steps, we have successfully configured and installed the Golang development environment and run the first Golang program. I hope this article can help readers smoothly develop Golang. You are also welcome to consult this article if you encounter problems when using Golang.

The above is the detailed content of Steps to set up and install Golang development environment on Mac computer. 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