Go language download and install go and vscode configuration go environment

In the previous article, please move to the download and installation of Go language and the first code_water w's blog-CSDN blog

Table of contents

1. Download, install and configure the go environment

1 Download and install go

2 Configure go environment

Two, install and configure git

1. Develop golang on vscode

1 configuration

2 Write code

解决报错:go: go.mod file not found in current directory or any parent directory; see 'go help modules'

Solve the error: .\main.go:4:2: undefined: fmt


1. Download, install and configure the go environment

1 Download and install go

(1) Download the Go installation package, address: Go language Chinese website , download according to the computer version, here I downloaded the windows version,

(2) First find the downloaded installation file,

Then double-click the msi file to install it, here I installed it in the path of C:\go\,

The directory after installation is as follows:

(3) Check whether the environment variable is added successfully,

It can be large or small. After the installation is complete, C:\go\binthe directory will be automatically added to the environment variable, and go can be used here.

(4) View the currently installed version through the cmd command line,

g version

 

ok, go has been installed successfully.

2 Configure go environment

(1) Turn on powersell,

(2)

Note : It is best to configure on the cmd command line,

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

Then I found out one thing, it seems that the env set in powershell is not working at all? !

After opening powersell, execute the following two commands to configure go,

$env:GO111MODULE = "on"
$env:GOPROXY = "http://goproxy.cn"

Enter the following command to check whether the configuration is successful,

go env

Two, install and configure git

(1) Here I have installed git,

(2) Enter git in cmd, execute the command,

git

A prompt will be returned, indicating that the git installation is successful.

1. Develop golang on vscode

1 configuration

Build a Golang development environment in vscode,

2 Write code

Create a new main.go file, write code, enter pkgm to quickly write, import fmt, and then enter fp to quickly write,

package main

import "fmt"

func main() {
	fmt.Println("hello go")
}

Right-click, select "run code", run the code,

ok, the operation is successful.

解决报错:go: go.mod file not found in current directory or any parent directory; see 'go help modules'

Error:

Reason: It may be because there is a problem with the part of go env

Solution:

(1) Open the cmd command line, enter and execute the next command, and enable the go modules function,

go env -w GO111MODULE=on

Then, you can see that Daxin has generated a mod file,

 

Solve the error: .\main.go:4:2: undefined: fmt

Error:

Reason: At the bottom right of the screenshot, vscode reminds you that you need to install the go plugin, but when you click install all to install, you will find that the installation will fail.

Reason: Check if import "fmt" is missing . In addition, the failure to install the plugin is because many dependent library files are automatically updated when installing the go plugin, which are all updated from Github , but because there are many files in Github The files in the official website of Go are applied, because some networks cannot be accessed in China, and because of the network, they cannot be downloaded directly, resulting in installation failure.

Solution:

(1) The core is to configure domestic download sources. We need to modify the following two go environment configurations:

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

So it seems that the env set in powershell in the previous steps has no effect at all? !

(2) After configuring the two variables, reopen VSCode, click install all at the bottom right to reinstall,

Or, use Ctrl+Shift+P in vscode, enter >go:install, and the relevant commands will be automatically searched below. We choose the command Go:Install/Update Tools, select all plug-ins, and click OK to install.

Problem solved successfully:

ok, problem solved.

Guess you like

Origin blog.csdn.net/qq_45956730/article/details/129022477