In 2023, the latest brainless installation of Go lang environment configuration and writing, running, and packaging of the first Golang program detailed steps, with graphic tutorials

The GO language, also known as Golang, is produced by Google and has grown with the development of cloud computing. In recent years, more and more programmers use the GO language. GO programmers are generally called GOphers. The GO language is similar to the C language in syntax, and the GO language has the following characteristics:

  1. Open source, open source and free is one of the reasons why many programmers choose GO language;
  2. Cross-platform, can be used on windows, Linux, MacOS and other platforms;
  3. The unique object-oriented programming method realizes the programming-oriented ability through structure type + association method;
  4. The richness of network functions should be realized slowly;
  5. The program can be run directly on the go playground on Google's website, you can directly write code or paste your own code, and then run it, so you can use the GO language without installing any software, and you can also run it on the following websites. https://play.studygolang.com/
  6. Simplicity in concurrent programming;
  7. Rich standard library;

Since the hard disk space of the blogger is relatively sufficient, the mobile software installation directory is not considered. If you need it, please find the method yourself. This article is a brainless operation.

Download and install Golang

Download URL: All Versions - Go Programming Language (google.cn)

image-20230510190956335

Click the first one under Featured Installation to download it. After the download is complete, execute the installation file, click Next for all, open the cmd command line, enter go versionthe pop-up version number, and the installation is successful.

Configure the Golang environment

GO111MODULE

We can simply understand that when there is no go modules mechanism, the management of third-party function packages in go projects is very complicated and very professional, which makes it difficult for programmers to manage third-party function packages when developing. Inconvenient, so there is a go modules mechanism. The switch of this mechanism is configured through the GO111MODULE environment variable.

GO111MODULE=off, no module support, the go command line will not support the module function, and the way to find dependent packages will follow the old version to search through the vendor directory or GOPATH mode.

GO111MODULE=on, module support, the go command line will use modules, and will not search in the GOPATH directory at all.

GO111MODULE=auto, the default value, the go command line will determine whether to enable the module function according to the current directory. This situation can be divided into two situations:

Note: If you don't need to use it later, you can set the value to off again.

GOPROXY

It can be seen from the literal meaning that GOPROXY represents the proxy settings of go. The reason for this environment variable is that the language of go is not like C language. In C language, if we want to use other people's third-party code , there are generally two ways:

In the go language, similar to java, you can introduce the library address of the third-party code, such as the git warehouse, when programming, and then when compiling, the IDE will automatically pull the third-party library file to the current project.
Although it is very convenient to do so, it brings a problem: network speed and limitation .
The default GOPROXY of golang is https://goproxy.io . This is the official setting. We can use domestic proxy. The settings under Windows are as follows:, Due to some restrictions, we cannot use and download these warehouses smoothly, which will lead to slow or failed downloads, so at this time, we need an agent to realize the download. This agent is a middleman and can be accessed across restrictions.

The default GOPROXY of golang is ** https://goproxy.io **, this is the official setting, we can use domestic proxy: https://goproxy.cn

Open the Go mod mode and set the package to download the domestic mirror

  1. Open advanced system settings

  2. Open the environment variables panel

    image

  3. Create a new system variable, variable name: GO111MODULE; variable value:on

    image-20230510192528482

  4. Create a new system variable, variable name: GOPROXY; variable value:https://goproxy.cn

    image-20230510192546338

  5. Click the OK button in the Environment Variables panel and the OK button for Advanced System Settings.

  6. Enter the cmd command go envto find out whether the corresponding variable is set successfully. Be careful not to use the cmd window that has been opened before setting the environment variable. There is a cache, and it is very likely that the setting is unsuccessful. You need to open a new cmd window for testing.

  7. If the test fails, enter respectively in the cmd command

    • go env -w GOPROXY=https://goproxy.cn,direct
    • go env -w GO111MODULE=on

Configure Vscode Golang environment

Vscode download address: https://vscode.cdn.azure.cn/stable/b4c1bd0a9b03c749ea011b06c6d2676c8091a70c/VSCodeUserSetup-x64-1.57.0.exe

  1. Execute the installation file and continue to the next step to complete the installation.

  2. Open the Vscode software and open the plugin installation location

    image(1)

  3. Search for go in the search bar to find the Go plugin and click Install to install the plugin, as shown in the figure below.

    image-20230510194640370

  4. Write a HelloWord program for testing

    Click the top button of Vscode, select Open Folder to open the code directory you want to place, the first one on the right of the directory folder in Vscode, create a new file hello.go, and enter the following code:

    package main
    
    import "fmt"
    
    func main() {
          
          
    	fmt.Println("Hello Word!")
    }
    

    Open the cmd window in the directory where the code is placed: enter cmd in the address bar of the corresponding folder to open cd to the cmd window of the folder, enter Hello, go run hello.goWord! That is, the operation is successful. Vscode installed successfully.

  5. Build the hello project

    Enter in the cmd command window just opened go build hello.go, and the hello.exe file will appear, which is the project file. Enter helloto run the file.

Bug collection

The “gopls” command is not available.Run “go get -v golang.org/x/tools/gopls” to install.

image-20230510195412123

Click install all. It can be displayed as follows.

image-20230510195821870

Guess you like

Origin blog.csdn.net/yumuing/article/details/130910649