Home  >  Article  >  Backend Development  >  Golang installation tutorial: Starting from scratch, teach you how to correctly install the golang development environment!

Golang installation tutorial: Starting from scratch, teach you how to correctly install the golang development environment!

WBOY
WBOYOriginal
2024-01-19 10:50:14869browse

Golang installation tutorial: Starting from scratch, teach you how to correctly install the golang development environment!

Golang installation tutorial: Starting from scratch, teach you how to correctly install the Golang development environment!

Introduction: With the explosive growth of Golang in recent years, more and more developers have begun to learn and use Golang for development. But for some beginners, installing the Golang environment may be a confusing process. This article will start from scratch and introduce in detail how to correctly install the Golang development environment, with specific code examples.

Step one: Download the Golang installation package
First, we need to download the Golang installation package. Golang officially provides multiple versions of installation packages, including Windows, Linux and macOS. We can choose the corresponding version of the installation package according to our operating system.

Installation packages for all versions can be found on Golang’s official website (https://golang.org). Select the corresponding operating system version and click download.

Step 2: Install Golang
After the download is completed, we can start installing Golang.

In Windows systems, we only need to double-click the installation package and then follow the prompts to install. By default, Golang will be installed in the root directory of the C drive.

In Linux systems, we can install it through the command line. First, open a terminal and switch to the directory where the installation package is located. Then, use the following command to install:

tar -C /usr/local -xzf go1.16.linux-amd64.tar.gz
export PATH=$PATH:/usr/local/go/bin

The "go1.16.linux-amd64.tar.gz" here is the file name of the downloaded installation package. Please modify it according to your own installation package file name. The function of these two commands is to decompress the installation package to the /usr/local directory and add the Golang execution file path to the system's PATH environment variable.

In macOS system, we can drag the installation package into the application folder, then open the terminal and execute the following command:

sudo tar -C /usr/local -xzf go1.16.darwin-amd64.tar.gz
export PATH=$PATH:/usr/local/go/bin

Similarly, here "go1.16. "darwin-amd64.tar.gz" is the file name of the downloaded installation package. Please modify it according to your own installation package file name. The functions of these two commands are the same as those in the Linux system.

Step 3: Configure Golang environment variables
After the installation is completed, we need to configure Golang's environment variables so that we can run Golang related commands in any directory.

In Windows systems, we can press the Win R key and enter "sysdm.cpl" to open the system properties window. Click the "Advanced" tab and then click the "Environment Variables" button. In "User Variables" or "System Variables", find the variable named "Path" and click the "Edit" button. Add the Golang installation path (default is "C:Go in") to the end of the variable value and click the "OK" button.

In Linux and macOS systems, we need to edit the ".bashrc" file in the user directory to add Golang environment variables. Open a terminal and execute the following command:

vi ~/.bashrc

In the open file, add the following code to the last line:

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

Save and exit the file. Then, execute the following command to refresh the variables in the file:

source ~/.bashrc

Step 4: Verify the installation results
After the installation is completed, we can verify the installation results by executing some simple commands.

Open the terminal and execute the following command:

go version

If the installation is successful, the terminal will display the Golang version number.

In addition, in Windows systems, we can use the following command to create a code file named "helloworld.go":

echo "package main

import "fmt"

func main(){
    fmt.Println("Hello, World!")
}" > helloworld.go

Then, execute the following command to run the code file:

go run helloworld.go

If everything goes well, the terminal will output "Hello, World!".

Conclusion: Through the introduction of this article, we learned how to correctly install the Golang development environment and performed some simple verifications. Now, you can start developing with Golang. I wish you more success in learning and using Golang!

The above is the detailed content of Golang installation tutorial: Starting from scratch, teach you how to correctly install the golang development environment!. 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