Due to the high performance and convenience of the Golang language, more and more programmers are beginning to use it, whether they are developing network applications, implementing system programming, or building data structures and algorithms. This article will introduce how to set up a Golang environment on a Windows system, hoping to help beginners get started quickly.
Installing Golang
First, you need to download the Golang installation file. It can be downloaded from the official website or from the Golang mirror website. It is recommended to download from the official website, because the official website provides the latest version of the installation program.
After the download is completed, double-click the installer and the Golang installation wizard will pop up. Follow the prompts to install step by step, select the functions to be installed and the installation directory. It is recommended to set the installation directory to the Go directory under the root directory of drive C to facilitate subsequent environment configuration.
Configure environment variables
After the installation is completed, you need to configure environment variables. Open your computer's Properties window: Right-click "This PC," select "Properties," then select "Advanced system settings."
In the pop-up "System Properties" window, select the "Advanced" tab, and then click the "Environment Variables" button. In "System Variables", click the "New" button, enter the variable name "GOPATH", and the variable value is the customized working directory path. For example, set GOPATH to D:go:
After clicking OK, find the "Path" variable in "System Variables" and edit it. Add ";C:go in" after the last directory in "Path" (that is, the "bin" directory under the Golang installation directory). An example is as shown below:
After saving the changes, close all windows. Now, open the command line window and enter the following command:
go version
If version information similar to this is output, it means that the Golang environment has been set up:
go version go1.13.4 windows/amd64
Add third-party library support
In Golang, third-party libraries are very important for development. In order to be able to introduce third-party libraries, you need to use Go Modules or the GOPATH environment variable. Here is the method of GOPATH:
First, create three subdirectories under the GOPATH directory: src, pkg and bin. The src directory is used to store all source code files, the pkg directory is used to store compiled library files, and the bin directory is used to store executable files:
mkdir -p %GOPATH%src%USERNAME%hello mkdir %GOPATH%pkg mkdir %GOPATH%in
Among them, %GOPATH% represents the one specified by the GOPATH environment variable Path, %USERNAME% represents the username of the current user.
Assuming that we need to use a third-party library now, we can use the following command to download it:
go get github.com/gin-gonic/gin
This command will download the gin library and place it in the %GOPATH%src directory. Later project files can use the following statement to import the gin library:
import ( "github.com/gin-gonic/gin" )
Summary
It is not difficult to build a Golang environment on a Windows system. You only need to install Golang first, then set the GOPATH environment variable, create the src, pkg, and bin directories in the GOPATH directory, and then you can start using Golang for development. Additionally, you can use the go get command to download and import third-party libraries.
In future development, we need to continue to learn and master more knowledge of Golang, and continue to accumulate experience in practice. I hope every Golang programmer can make full use of this concise and efficient language.
The above is the detailed content of Golang environment to build windows. For more information, please follow other related articles on the PHP Chinese website!