Install golang under win11

Backed by the go language of major foreign sponsors, it can be said to be a new wave under various promotions. However, although there are various promotions in China, there are very few materials from installation to development to maintenance, which may be monopolized Right to explain. Therefore, the following is just a record, and it is a successful attempt limited to this stage of my own machine.

1. Golang installation under win11

The go language has a personal community, but in foreign countries, accessing it is equivalent to visiting Google, and the connectivity is almost negligible, so my installation source is the go language Chinese website. as follows:

go language Chinese website

It seems that this can also

Go official website download page

As follows, it is already the go1.19 version, select the installer option of windows, click to download the msi package, this is easier, because the downloaded one is an installation software (note the version, it is said that it is basically a 64-bit system now).Go language Chinese website download page

Click to run the installation software, the installation steps are as follows, just follow the process:Installation software initial interface

Because I have already installed it, this time it is uninstalled and reinstalled. If you have not installed it, you will not have this pageReinstall prompt

The following basic process is "agree to the agreement", "select the installation path", and then wait for the installation to complete.
insert image description here
insert image description here
insert image description here
Even if the installation is complete here, the installation of go will not help us configure the environment path, so we need to configure it ourselves.

[Settings] - [System Information] - [Advanced System Settings], then click [Environment Variables], the computer is generally used by itself, there is no multi-user setting, and the installation is also performed in this user, so just edit the user variables directly .
Click the PATH variable and add the go executable file just now (that is, the path where go.exe is located), the steps are as follows:insert image description here

insert image description hereinsert image description here

After the above steps are completed, the system "knows" that we have a go-runnable software, let's open the command line interface to check the installation results.
【win+r】Shortcut key input "cmd", open the command line, and then enter the command to check the go version. The result is as follows:

C:\Users\user>go version
go version go1.19.2 windows/amd64

C:\Users\user>

It can be seen that the go version is the latest 1.19.2, which happened to be installed by us, so even if the installation is complete, then test it and use it, let's make a simple test.go.

package main

import "fmt"

func main() {
    
    
    fmt.Println("Hello, I'm your father.")
}

Simply run it, there are two ways, one is to run directly, the other is to compile and generate an executable file, and then run the executable file. as follows:

PS D:\Desktop> go run test.go
Hello, I' m your father.
PS D:\Desktop> go build test.go
PS D:\Desktop> ./test
Hello, I' m your father.
PS D:\Desktop>

2. Configure the go development environment

Configure GOPATH and GOROOT

GOPATH is the project path of go, which is used to store some personal libraries, compiled files and source code; GOROOT is the installation path of go, which is the location of some standard libraries, compiled software and some important software of go. The same is done by editing the environment variables.

Just add it as follows:
GOPATH and GOROOT

Sometimes there will be no immediate results after the modification. We can enter the go env command in the command line window to check. This is a command to check the environment parameters of go. If there is no modification, you need to restart the computer and look again.

Then modify the go agent, because the domestic access is not available, another address is needed, and the cmd is modified

PS D:\Desktop> go env
set GO111MODULE=off
set GOARCH=amd64
set GOBIN=
set GOCACHE=C:\Users\penta\AppData\Local\go-build
set GOENV=C:\Users\penta\AppData\Roaming\go\env
set GOEXE=.exe
set GOEXPERIMENT=
set GOFLAGS=
set GOHOSTARCH=amd64
set GOHOSTOS=windows
set GOINSECURE=
set GOMODCACHE=D:\documents\gocourse\pkg\mod
set GONOPROXY=
set GONOSUMDB=
set GOOS=windows
set GOPATH=D:\documents\gocourse
set GOPRIVATE=
set GOPROXY=https://proxy.golang.org,direct
set GOROOT=D:\software\go
set GOSUMDB=sum.golang.org
set GOTMPDIR=
set GOTOOLDIR=D:\software\go\pkg\tool\windows_amd64
set GOVCS=
set GOVERSION=go1.18.7
...
PS D:\Desktop> go env -w GOPROXY=https://goproxy.cn,direct
PS D:\Desktop> go env
...
set GOPROXY=https://goproxy.cn,direct
...
PS D:\Desktop>

go env is a configuration command for viewing go. Use go env -w to rewrite the configuration. When you start to modify, the env file will be generated on the path specified by the GOENV parameter above. You can also see the modified one above GOPATH and GOROOT parameters. After running go env -w to modify GOPROXY, you can see that it has been modified to the required proxy address. There are too many parameters in go env, and only some required reference parameters are shown above.

3. Configure the vscode environment

According to the mainstream, most people use Jetbrain's goland for go development and debugging, but personally use vscode, and the configuration here is also for vscode.

The configuration of vscode is relatively simple. In summary, it is to install a corresponding plug-in, and install go tools under the GOPATH path.

First search for go in the go plugin bar, find the corresponding plugin, and click Installinsert image description here

After installation, open the command panel of vscode ([View]-[Command Panel]), then enter the go install tools command, and then select all of them to install. Many times it will fail because of network problems, because many packages are in Maintained on github.insert image description here

What to do when it fails

  • Create the golang.org/x directory in the src directory under the GOPATH directory
  • In the newly created x directory, right click to open the terminal, then git clone two packages, mainly enter the following command
git clone https://github.com/golang/tools.git
git clone https://github.com/golang/lint.git
  • Then go back to vscode, install it again, and you're done.

After success, the interface is as follows:

Tools environment: GOPATH=D:\documents\gocourse
Installing 7 tools at D:\documents\gocourse\bin in module mode.
  gotests
  gomodifytags
  impl
  goplay
  dlv
  staticcheck
  gopls

Installing github.com/cweill/gotests/gotests@latest (D:\documents\gocourse\bin\gotests.exe) SUCCEEDED
Installing github.com/fatih/gomodifytags@latest (D:\documents\gocourse\bin\gomodifytags.exe) SUCCEEDED
Installing github.com/josharian/impl@latest (D:\documents\gocourse\bin\impl.exe) SUCCEEDED
Installing github.com/haya14busa/goplay/cmd/goplay@latest (D:\documents\gocourse\bin\goplay.exe) SUCCEEDED
Installing github.com/go-delve/delve/cmd/dlv@latest (D:\documents\gocourse\bin\dlv.exe) SUCCEEDED
Installing honnef.co/go/tools/cmd/staticcheck@latest (D:\documents\gocourse\bin\staticcheck.exe) SUCCEEDED
Installing golang.org/x/tools/gopls@latest (D:\documents\gocourse\bin\gopls.exe) SUCCEEDED

All tools successfully installed. You are ready to Go. :)

Create a new go file for testing to see if there are normal code prompts and formatting tools.

The above is a personal installation record, for reference only

Guess you like

Origin blog.csdn.net/weixin_44948269/article/details/127626316