[go language series] configure the development environment of go in vscode

Time:2021-1-20

1、 Why vscode

The beginning of this series is to lead the company’s PHPer to go. Before writing this blog, let’s talk about the mainstream ides of go

1. GoLand (charge)

JetBrains products must be a boutique, in addition to expensive, no other shortcomings, if you use this IDE, I just want to say go out and turn right, no free! ~~~~

2. Liteide (free)
Liteide is a simple open source ide. It is worth noting that it is the first ide of go language released in 2012, developed by QT. Since it is designed directly for golang, Lite ide provides developers with many useful features, including configurable build commands, advanced code editors, and extensive golang support. Other functions include code management, GDB and delve debuggers, automatic completion and use of wordapi topics, mime based systems, etc. In fact, for novices, I generally recommend using Lite ide. Although it’s free, it can work. The most important thing is to install it with one click. There’s no need to be distracted by configuration. But I can’t really love this logo. Beauty is justice.

3. Eclipse (free)

Goclipse is a plug-in to eclipse. With goclipse plug-ins, developers can program with the popular eclipse ide. Both eclipse IDE and goclipse plug-ins are free and open source. Goclipse editor provides developers with a wide range of functions, including source code editor, project wizard and builder, to help report build errors within the editor, as well as full-featured GDB debugger support. The famous eclipse, when I wrote Java in college, I couldn’t love it! twenty-three thousand three hundred and thirty-three

4. Atom (free)

Developers can take advantage of the atom IDE’s improved language integration and smarter editor. The open source go plus software package makes it easier for developers to program in go. Atom and go plus software package provide tools, build process, linkers, vet and coverage tools for golang. Other features include autocomplete, format, test and document. You can add other debugging functions by using deve’s go debug package. Atom + go plus has enough functions and fairly good performance, which is worth selling.

5. VIM (free)

VIM has many plug-ins to help developers edit their go code more easily. The VIM go plug-in automatically installs everything necessary to provide smoother integration for go developers in vim. VIM go has many useful features, including compiler, improved syntax highlighting and folding, completion support, and a series of debuggers with integrated support. There are also some advanced source analysis tools used, including: goimplements,: gocallees, and: goreferrers. Other VIM plug-ins include syntax plug-in for feedback compiler errors, tagbar plug-in for gotags, VIM compiler plug-in for syntax checking, and even VIM bootstrap for. Virmrc configuration. To tell you the truth, it has powerful functions. It has all kinds of functions. Its performance is very good. But there are too many shortcut keys. I can’t remember the interface. Have you ever used DOS? Yes, it’s white, black and red!

6. Vscode (free)

It is a popular open source ide developed by Microsoft. It has an out of the box go extension for vs code. The vscode go plug-in provides more functionality for developers, including integration with many go tools. Vs Code provides intelligent completion function through IntelliSense, built-in git integration, debugging code directly from editor and other functions. Vs code is highly extensible and provides many custom options through its many extensions. It also provides support for dozens of languages, which makes it a popular tool for developers. Average performance, a little bit more memory, perfect interface. And the previous PHP development also used this.

 

2、 What is go

Go is an open source programming language that makes it easy to build simple, reliable, and efficient software

Go language has the advantages of “simple deployment, good concurrency, good language design and good execution performance”. It originated in 2007 and was officially released in 2009.

The main goal of go language is “to have the development speed of dynamic languages such as Python and the performance and security of compiled languages such as C / C + +”.

Go language not only allows you to access the underlying operating system, but also provides powerful native network programming and concurrent programming support. Many well-known open source projects are implemented with go, such as docker, go Ethereum, thraform and kubernetes.

Go language is “the C language of Internet Era”. Go inherits many ideas from C language, such as similar expression syntax, control flow structure, basic data type, calling parameter value, pointer and so on, as well as the running efficiency of compiled machine code and seamless adaptation with existing operating system that C language has always been looking for.

Go language has no concept of class and inheritance. It doesn’t look the same as Java or C #. But it implements polymorphism through the concept of interface.

Go language has a clear and easy to understand lightweight type system, and there is no hierarchy between types. Therefore, it can be said that go language is a mixed language.

 

3、 Install go

1. Download

Open the official website https://golang.google.cn/ Click downloads go to enter the download page and find the download place, as shown in the figure below:

 

For the following installation, I will take win10 64 bit and centos7 64 bit as examples, so I will download the first and third items respectively

 

2. Install under Windows

Double click the installation package you downloaded (go1.13.4. Windows-amd64. MSI), and click “next” all the time. Note that there is a step to choose the installation path. Please do not change it for the good instructions of the following tutorial.

 

 

Then click “Install” to install

 

Please wait for this process

 

Click “finish” to complete the installation

 

How to verify that our installation is successful? Press windows key + R, enter: CMD, enter

 

In the command line window that appears, enter: go version, and the go version you currently installed will be displayed, indicating that we have successfully installed

 

3. Installation under CentOS

Just download the file (you can also use the $WGet command to download) to the specified directory, my use is / home / Roy / go, open the terminal, enter the directory, as shown in the figure

Install go and execute the command

$tar -xvf go1.13.4.linux-amd64.tar.gz

Set environment variables and execute commands

$vim /etc/profile

Append at the end of the file (press I to enter insert mode)

export GOROOT=/home/roy/go/go
export GOPATH=/home/roy/go/gopath
export PATH=$PATH:$GOROOT/bin:$GOPATH/bin

Save, press ESC, and then enter: WQ enter

Test whether the environment is successful, restart the computer, and input the following on the terminal:

go version

The following figure shows success

 

To write a simple code try to create a directory and enter the directory

$cd /home/roy/go/gopath
$mkdir helloworld$cd helloworld

create a file main.go

$vim main.go

input

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

Save, press ESC, and then enter: WQ enter

implement

$go run ./main.go

The successful output is shown in the figure below

 

 

4、 Install vscode

1. Download

Open the official website https://code.visualstudio.com/Download After entering the download page, find the download place, as shown in the figure below:

Win10 64 bit click the first red arrow to download, CentOS 64 bit click the second arrow to download

 

2. Installation under Windows

Double click the installation program you downloaded, and then click all the way in the order shown below

 

 

 

 

 

 

 

 

 

 

 

3. Installation under CentOS

Download the. RPM of vscode on the official website to get the file code-1.40.2-1574694258.el7.x86_ 64. Rpm, move it to / home / Roy / vscode directory, and enter this directory under the terminal

$cd /home/roy/VSCode

Perform the installation

rpm -ivh code-1.40.2-1574694258.el7.x86_64.rpm

At this time, an error is reported, as shown in the figure

 

Execute the following command:

yum install libXss*  -y

After success, perform the installation again

rpm -ivh code-1.40.2-1574694258.el7.x86_64.rpm

 

 

The installation is successful. At this time, we can find vscode in the application menu

 

 

 

5、 Install vscode Chinese package

If your English is very low like the author, you can choose to install the Chinese language pack

First, you can click the fifth icon on the left, or press the shortcut key Ctrl + Shift + X to open the extensions, enter the package in Chinese (Simplified) in the search box, and then click “Install” to install, as shown in the following figure

 

Open vscode again, it is already in Chinese

 

6、 Install go plug in

Open extensions, type go, and install the Microsoft version

 

7、 Install git tools

1. Open https://git-scm.com/downloads , select the installation file of your system to install

 

 

2. After downloading, if you have a Windows system, double-click the installation package all the way to next. If you are Linux, Git is installed by default. If you don’t have git, please go to Baidu by yourself

 

8、 The solution to the wall

1. Set goproxy to https://goproxy.cn

Open command line execution

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

 

2. Mirror source using GitHub

Golang builds a mirror library on GitHub, such as https://github.com/golang/net That is https://golang.org/x/net Image library of

obtain golang.org/x/net In fact, the package only needs the following steps:

git clone https://github.com/golang/net.git

Then copy the GIT folder to the$GOPATH/src/golang.org/xTake windows as an exampleC:\Users\Administrator\go\src\golang.org/xOf course, you can also first CD to the directory, and then execute GIT

 

3. Get tools from mirror source

Many go softwares need to use the contents of tools when compiling. Use the following methods to obtain the contents (take windows as an example)

cd \
cd C:\Users\Administrator\go\src\golang.org\x
git clone https://github.com/golang/tools.git

 

 

4. Gopm instead of go to download the third-party dependency package (this method is not used in the configuration of this tutorial)

Gopm can be used to golang.org Some images can be downloaded from the website

Install gopm

go get -u github.com/gpmgo/gopm

Replace go getgopm get with gopm get – G

gopm get -g golang.org/x/net

Without the – g parameter, the dependent package will be downloaded to the. Vendor directory; with the – g parameter, the dependent package can be downloaded to the gopath directory;

 

9、 Install vscode go

For visual studio code development tool, there is an excellent golang plug-in. Its home page is: https://github.com/microsoft/vscode-go
Features of this plug-in include:

  • Colorization code is colored
  • Complete lists code automatically (using gocode)
  • Snippets
  • Quick Info prompt message (using godef)
  • Goto definition jump to definition (use godef)
  • Find references search for reference references (use go find references)
  • File outline (use go outline)
  • Workspace symbol search (using go symbols)
  • Rename (use gorename)
  • Build on save saves the build (using go build and go test)
  • Format code format (using goreturns or goimports or gofmt)
  • Add imports add references (use gopkgs)
  • Debugging debugging code (using delve)

Integrate the installation command and copy it to the CMD window to complete the installation

go get -u -v github.com/nsf/gocode
go get -u -v github.com/rogpeppe/godef
go get -u -v github.com/golang/lint/golint
go get -u -v github.com/lukehoban/go-find-references
go get -u -v github.com/lukehoban/go-outline
go get -u -v sourcegraph.com/sqs/goreturns
go get -u -v golang.org/x/tools/cmd/gorename
go get -u -v github.com/tpng/gopkgs
go get -u -v github.com/newhook/go-symbols

 

Let’s take windows as an example

1. Enter the command window and enter:

go get -u -v github.com/nsf/gocode

Installation successful

 

2. Enter the command window and enter:

go get -u -v github.com/rogpeppe/godef

Installation successful

 

3. Enter the command window and enter:

go get -u -v github.com/golang/lint/golint

Installation failed, unable to install“ golang.org/x/lint/golint “As everyone knows, there are always some mistakes in installing go packages in Chinese mainland. We use eighth ways to install the” first package “. golang.org/x/lint/golint ”, input:

git clone https://github.com/golang/lint.git

After success, execute again

go get -u -v golang.org/x/lint/golint

After success, execute again

go get -u -v github.com/golang/lint/golint

 

 

Installation successful

 

4. Enter the command window and enter:

go get -u -v github.com/lukehoban/go-find-references

 

Installation successful

 

5. Enter the command window and enter:

go get -u -v github.com/lukehoban/go-outline

 

Installation successful

 

6. Enter the command window and enter:

go get -u -v sourcegraph.com/sqs/goreturns

Installation successful

 

7. Enter the command window and enter:

go get -u -v golang.org/x/tools/cmd/gorename

 

Installation successful

 

8. Enter the command window and enter:

go get -u -v github.com/tpng/gopkgs

 

Installation successful

 

9. Enter the command window and enter:

go get -u -v github.com/newhook/go-symbols

 

Installation successful

 

10、 Test installation

1. Create a new folder gohello in gopath, open vscode, open this folder, and create a new file main.go , enter the following code in the file

package main

import (
	"fmt"
)

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

If there is a prompt in the lower right corner that you need to update some plug-ins, you can ignore it or directly click Update, as shown in the figure: your version of go outline looks to be out of date. Please update for an improved experience

I’ll just click Update here

 

Update succeeded

 

Similarly, if you are prompted to have a package to install, you can directly click “Install” to install it. Note: it is better to restart vscode after installing or updating package

 

This environment has been configured. This environment has common functions such as code completion, intelligent perception, automatic code formatting (Ctrl + s), code tracking (F12), breakpoint (F5), running (Ctrl + F5), etc