Recently, I worked with a friend on a project where the back end was written using Golang. So I started playing with Golang again, and then I looked at the paperwork, which I hadn’t written for over a year.

The Golang community has been updated to 1.13.8, while mine is still 1.11.2. So I pulled up the previous configuration records. After I finished, I found that the difference was a little big from a year ago. There are about 8 more modules, including syntax hints, module support, reference tracking, jump definition, jump implementation, automatic import management, etc.

The process took a while, and there were some bumps, such as some modules only support x64, some modules don’t work properly with Go get, etc.

All right, let’s cut to the chase.

01 – System environment

Pre-knowledge: System/user environment variables

  • GOROOT Go source directory. For mSI installation package, the default directory is C :/ Go.
  • Workspace for a GOPATH Go project/project, which contains three main directories (all lowercase directory names)
  • Bin Directory of executable files after compilation/build. (the Executable File)
  • PKG Directory of archive files. (Archive File)
  • SRC Source file directory of the project/project. (the Source File)
  • The directory where the GOCACHE Go command will store cache information for reuse in future generation.
  • The directory in which the GOTMPDIR Go command will write temporary source files, software packages, and binaries.

I will not repeat how to configure the Go development environment here. If you don’t know much about Golang, you can refer to: What you need to know before you enter

02 – Development support environment

The supporting modules required for the VS Code development environment are all installed to generate 17 executable files. (Take Windows as an example here)

golang.org/x/

The directory must be %GOPATH%/ SRC /golang.org/x/. Clone the support packages separately.

git clonehttps://github.com/golang/tools.git (tool sets and package source) gitclonehttps://github.com/golang/lint.git (lint everyone not strange) gitclonehttps://github.com/golang/mod.git (mod module support) gitclonehttps://github.com/golang/xerrors.git (error value conversion package)Copy the code

go install

You need to install the support module manually. You are advised to run the command in %GOPATH%. Otherwise, the command may be installed in another path.

go install golang.org/x/lint/golint
go install golang.org/x/tools/cmd/guru
go install golang.org/x/tools/cmd/gorename
Copy the code

Other packages (install | | import)

Install or import is required, but install is recommended. Installation method is the same as above.

git clonehttps://github.com/golang/net.git (network packet) gitclonehttps://github.com/golang/sync.git (synchronous/atomic package) gitclonehttps://github.com/golang/crypto.git (encryption package) gitclonehttps://github.com/golang/debug.git (debug toolkit) gitclonehttps://github.com/golang/oauth2.git (oauth2 authentication package) gitclonehttps://github.com/golang/protobuf.git (protobuf package agreement)Copy the code

These are some of the additional and commonly used source code extension packages, which can be considered if necessary, and of course can be implemented by third parties.

github.com/

Needs to be placed under %GOPATH%/src/github.com/. You can install it in go get mode

go get -u -v github.com/uudashr/gopkgs/cmd/gopkgs go get -u -v github.com/ramya-rao-a/go-outline go get -u -v github.com/rogpeppe/godef go get -u -v github.com/godoctor/godoctor go get -u -v github.com/acroca/go-symbols go get -u -v github.com/fatih/gomodifytags go get -u -v github.com/haya14busa/goplay/cmd/goplay go get -u -v github.com/davidrjenni/reftools/cmd/fillstruct go get -u -v github.com/cweill/gotests/... (Three points is not a mistake, really.)Copy the code

Abnormal condition

If go GET cannot be used, use Clone and go Install. Run the %GOPATH% command.

Go get -u -v github.com/sqs/goreturns (if the installation fails, use the following command) gitclone https://github.com/sqs/goreturns.git
go install github.com/sqs/goreturns.git
Copy the code
Go get - u - v github.com/josharian/impl (can't normal installation using the following commands) gitclone https://github.com/josharian/impl.git 
go install github.com/josharian/impl
Copy the code

DLV is more important, if the SYSTEM is X64 or recommended to install this module, this module can be directly debug go code through the development tools or editor tools. (This support module only supports x64 version, pay attention to the system version and Golang version)

Go get -u -v github.com/go-delve/delve/cmd/dlv (if the installation fails, use the following command) gitclonehttps://github.com/go-delve/delve.git (you need to manually create the go-delve directory, and then put delve in it) go install github.com/go-delve/delve/cmd/dlvCopy the code

Gocode (core – syntax related)

# Go syntax with autocomplete, gocode-gomod needs to be prompted with VS Code when the installation is complete.https://github.com/stamblerre/gocode.git (support module) go get - u - v github.com/stamblerre/gocodeCopy the code
https://github.com/mdempsky/gocode.git (Go > 1.8 does not support module) Go get - u - v github.com/mdempsky/gocodeCopy the code
https://github.com/nsf/gocode.git (old version, and no longer maintenance) go get - u - v github.com/nsf/gocodeCopy the code

03 – VS Code configuration

VS Code needs to make some configuration changes before these modules are fully functional. File -> Preferences -> Settings -> settings.json Add the following

{
    "go.buildOnSave": "workspace"."go.lintOnSave": "package"."go.vetOnSave": "workspace"."go.coverOnSave": false."go.lintTool": "golint"."go.useCodeSnippetsOnFunctionSuggestWithoutType": true."go.useCodeSnippetsOnFunctionSuggest": true."go.autocompleteUnimportedPackages": true."go.gocodePackageLookupMode": "go"."go.inferGopath": true."go.docsTool": "gogetdoc"."go.formatTool": "goreturns",}Copy the code

These are the configurations I’m comfortable with, and you can change them to your own. For example, search for the configuration tool go.docstool to see the values of the configuration, and then try it yourself.

04 – Final situation

All modules will be installed in %GOPATH%/bin, so the final 17 modules will be:

dlv.exe                              gomodifytags.exe
fillstruct.exe                       gopkgs.exe
go-outline.exe                       goplay.exe
go-symbols.exe                       gorename.exe
gocode-gomod.exe                     goreturns.exe
gocode.exe                           gotests.exe
godef.exe                            guru.exe
godoctor.exe                         impl.exe
golint.exe
Copy the code

05 – Supplementary information

The supplementary content is also the problem found when writing the code recently

  1. gurugogetdocAre code description tips, recommended to usegogetdoc
  2. godoctor No longer updateCan be used without consideration of installation, alternative isgolint
  3. goimportsgoreturnsIs for formatting and package reference management, recommendedgoreturns
  4. github.com/stamblerre/gocodeThis supportgocode modPackage has aBug Traction too many resources very slow, occupy a lot of CPU resources, do not install the use!!

Gopls is recommended for go mod mode development!! The VS Code plugin will prompt you to install the go project when it finds that it is using mod mode.

06 – go mod & gopls

If you want to use the Go mod, configure GOPROXY first

One of the domestic mirror agent:

  • Seven cattle: GOPROXY =https://goproxy.cngoproxy.cn
  • Ariyun: GOPROXY=https://mirrors.aliyun.com/goproxy

Configuration GOPROXY

Configuration Mode Windows

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

# Windows 2
# 1. Open and edit system environment variables
# 2. Add GOPROXY variable name
# 3. Fill in the variable value https://goproxy.cn
# 4. Identify and restart the console
Copy the code

Configuration Mode macOS & Linux

# macOS & Linux 1
$ export GO111MODULE=on
$ export GOPROXY=https://goproxy.cn

# macOS & Linux 2
$ echo "export GO111MODULE=on" >> ~/.profile
$ echo "export GOPROXY=https://goproxy.cn" >> ~/.profile
$ source ~/.profile
Copy the code

Install gopls

Install GOPLS only after GOPROXY is configured

Manual installation

go get golang.org/x/tools/gopls@latest Get the latest version of GOPLS
Copy the code

Automatic installation

  1. Open the vscode
  2. Open the vscode console
  3. The inputgo mod init v2, using the V2 version of the mod
  4. Wait for vsCode at the lower right corner to prompt that there is a plug-in that can be installedgoplsAnd then click OKinstall
  5. Wait until the automatic command execution is complete

Configuration vscode gopls

VS Code needs to modify the configuration file -> Preferences -> Settings -> settings.json to get gopls to work.

"go.useLanguageServer": true."[go]": {
    "editor.formatOnSave": true."editor.codeActionsOnSave": {
        "source.organizeImports": true,},// Optional: Disable snippets, as they conflict with completion ranking.
    "editor.snippetSuggestions": "none",},"[go.mod]": {
    "editor.formatOnSave": true."editor.codeActionsOnSave": {
        "source.organizeImports": true,}},"gopls": {
     // Add parameter placeholders when completing a function.
    "usePlaceholders": true.// If true, enable additional analyses with staticcheck.
    // Warning: This will significantly increase memory usage.
    "staticcheck": false,},Copy the code

The last

As you can see, most plug-ins can’t be installed in the normal way, but of course, if you’re a Geek on the Internet, you really don’t have to worry about these problems, and most likely you won’t encounter them.

I also encountered a lot of problems in the process of trying, and then slowly to understand, intercept data, analysis, ask foreigners, and then solve these problems, finally through the normal way so that VSCode can have a complete support from the plug-in to support the complete Experience of Golang writing.

I appreciate it if I can help you, and if you have any other problems along the way, please leave a comment with a description of the operation and a screenshot of the key parts. As much as possible for you to answer.

At the end

Thank you for watching

🏆 technology project phase ii | and I Go those things…

Copyright: this article belongs to the author Lin Lin Lin Xiaoshuai, shall not be reproduced without authorization and second modification. Reproduced or cooperation please leave a message and contact information in the background.