Build a go language development environment based on VS Code (win10)

download link

Go official website download address: https://golang.org/dl/
Go official mirror station (recommended): https://golang.google.cn/dl/


Click on the website to enter the following interface for version selection and download
go official website

Install based on win10 system

In this installation example 64位win10system installation Go1.15.2可执行文件版本example.

Double-click the downloaded installation package in the previous step to install
Insert picture description here
Insert picture description here
Insert picture description here
Insert picture description here
Insert picture description here
it. After the installation is complete, open the terminal and enter to go versioncheck whether the installation is successful. If the Go version number appears, the installation is successful.
Insert picture description here

GOROOT and GOPATH environment variable configuration

After successful installation, environment variables need to be configured. Both
GOROOTand GOPATHare environment variables. Among them GOROOTis the path where we installed the go development package. Starting from the Go1.8version, the Go development package will GOPATHset a default directory after the installation is complete . GOPATHIt is our future working directory, and the code files when we write the project will be placed in that working directory. Here I create a new goworkspacefolder in the root directory of Disk D as my future working directory, so I need to go to the control panel to modify the environment variables and change GOPATHthe default path to the path of the newly created folder.

Modify environment variablesGOPATH
Insert picture description here
Insert picture description here

We only need to remember that we modify the GOPATHpath where you can, and by default GOROOTunder the bindirectory has been added to the environment variable, and we do not need the additional configuration.

Go1.14After the version, it is recommended to use the go modmode to manage the dependent environment, and it is no longer mandatory to write the code in the GOPATHfollowing srcdirectory (the general structure of the project will be explained later), you can write the gocode anywhere on your computer .

The default GoPROXYconfiguration is:, GOPROXY=https://proxy.golang.org,directbecause it is not accessible in China https://proxy.golang.org, we need to change one PROXY. It is recommended to use https://goproxy.ioor here https://goproxy.cn.

You can execute the following command to modify GOPROXY:

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

After the modification, enter go env in the terminal to view the go environment configuration and check whether the modification takes effect.

Insert picture description here

VS Code configuration and related plugins for go language

VS CodeThe full name Visual Studio Codeis a free modern and lightweight code editor open sourced by Microsoft. It supports syntax highlighting, smart code completion, custom hotkeys, bracket matching, code snippets, code comparison Diff, and almost all mainstream development languages. GIT and other features, support plug-in extension, support Win, Mac and Linux platforms.

Although not as powerful as some IDEs, it is sufficient for our daily Go development after adding Go extension plug-ins. The following describes how to add Go extension plugins and perform some simple configurations on VS Code.

Search for Go in the app store, and the plug-in in the red box in the figure below will appear, just click to download.
Insert picture description here

Since the proxy GOPROXY variable has been modified to an address we can access before, VS Code will not fail to download when automatically installing related plug-ins. As shown in the following figure, 17 Tools related plug-ins have been downloaded and installed successfully.
Insert picture description here
Add a newbie problem: repair the record after
installation VS Code, the first code will have package maina wavy line, and it go buildwill report an error directly. The main reason is that go envin the environment variables set set GO111MODULE=on, this means that the go mod initmodule is used. Just set this set GO111MODULE=autoto. In this way, there will be no wavy lines when the code is executed, and the go build command can also be executed

go env -w GO111MODULE=auto

Project structure

During the time go language development, our code will always be stored in $GOPATH/srcthe directory in the project through go bulid, go installand go getafter instruction, etc., will be downloadable third-party package source files in $GOPATH/srcthe directory, binary executable files generated by release In the $GOPATH/bindirectory, the generated intermediate cache file is placed in the $GOPATH/pkgdirectory.

The following are several common project structures

  • Structure for individual developers
    Insert picture description here
  • Current popular structure
    Insert picture description here
  • Structure suitable for enterprise developers
    Insert picture description here

The first go program

1. Create a hello folder in the src/github.com/github-user/ directory of the working directory.

Insert picture description here

2. Create a hello.go file in the hello folder and write the following code.

package main  // 声明 main 包,表明当前是一个可执行程序

import "fmt"  // 导入内置 fmt 包

func main(){
    
      // main函数,是程序执行的入口
	fmt.Println("Hello World!")  // 在终端打印 Hello World!
}

3. Use the terminal to compile and run.

  • go build

go buildIndicates that the source code is compiled into an executable file.

Execute in the hello directory:

go build

Or execute the following commands in other directories:

go build hello

The go compiler will go to GOPATHthe src directory to find the helloproject you want to compile. The compiled executable file will be saved in the current directory where the compilation command is executed. If it is the windowsplatform, the hello.exeexecutable file will be found in the current directory .

The hello.exefile can be executed directly in the terminal .

We can also use the -o parameter to specify the name of the executable file obtained after compilation.

go build -o 1.exe
  • go install

go installIndicates the meaning of installation. It first compiles the source code to get the executable file, and then moves the executable file to GOPATHthe bin directory. Because GOPATHthe bin directory is configured in our environment variables , we can directly execute executable files anywhere.

Guess you like

Origin blog.csdn.net/weixin_45960131/article/details/108505575