7

Install and Configure Go Env Via VSCode in Linux

 1 year ago
source link: https://en.liqiang.io/post/install-and-configure-go-env-via-vscode-in-linux?lang=US_EN
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
neoserver,ios ssh client

0. Overview

Introduces the general Linux environment installation method of VSCode and the process of configuring a Go developer environment.

1. Installation

1.1. Download the executable file

Open the official website and download the corresponding tarball: download address

1.2. Unzip the executable file



  1. [[email protected]]# tar -zxvf code-stable-x64-1602601238.tar.gz -C /opt/
  2. [[email protected]]# chmod +x /opt/VSCode-linux-x64/code
  3. [[email protected]]# ln -s /opt/VSCode-linux-x64/code /usr/local/bin/code

1.3. Start vscode



  1. [[email protected]]# code
Figure 1:vscode open page
44149f403f87.jpg

2. Configure Remote Developer

2.1. Install client plugin

Using:Ctrl + Shift + P open command tool,and execute following two commands:



  1. [[email protected]]# ext install ms-vscode-remote.vscode-remote-extensionpack
  2. [[email protected]]# ext install ms-vscode-remote.vscode-remote-extensionpack

2.2. Install server plugin

VSCode A variety of remote tools are supported, I chose the SSH method, in addition to SSH there are:

  • Container
  • Github Codespaces

Assume here that my remote machine is:10.0.63.154



  1. [[email protected]]# ssh-copy-id [email protected] # Configure loginwith password

2.3. Connect remote machine

3. Install Go Develop Environment

3.1 Install Go Plugin

Using shortcut Ctrl + Shift + X to open the plugin page,and search Go plugin,then click install:

Figure 2:Install Go lang plugin
5a5f53f55d74.png

3.2 Install Go Tools

Open the command tools windows by shortcut Ctrl + Shift + P,Search “Go: Install/Update Tools”

Figure 3:Install Go Tools
f550b358382c.png

And then select all,Click “OK” and then install:

Figure 4:Choose Go Tools
e8b7a7efa9ca.png

4. Debug Go Application by dlv

Now you can start configuring the Go debugging tools.

4.1 Configure file

Open the debugging tool via the shortcut key Ctrl + Shift + D and find the link inside:

Figure 5:Add debug configure
7007cdc24671.png

If you can’t find it,you can also open the .vscode directory below the root directory,and create a new file named:launch.json,wirte the following content into the file:



  1. [[email protected]]# cat .vscode/launch.json
  2. {
  3. "version": "0.2.0",
  4. "configurations": [
  5. {
  6. "name": "Launch",
  7. "type": "go",
  8. "request": "launch",
  9. "mode": "auto",
  10. "program": "${workspaceFolder}/apps/main.go",
  11. "env": {
  12. "env":"test"
  13. },
  14. "": "envFile\": \"${workspaceFolder}/.env",
  15. "args": []
  16. }
  17. ]
  18. }

Now, you have finish your configure。

4.2 Add breakpoint

It’s so easy to add the breakpoint,just click you mouse near the code line:

Figure 6:Add breakpoint
b9f0a4aa5c44.png

4.3 Begin debug

You can start to debug you code via shortcut F5,and can also open the debug window and click Run button to debug:

Figure 7:Debug Code
9fae0de02757.png

Then, after stopping at the breakpoint, you can choose whether to step or skip:

Figure 8:Debug method
a5064e5c3d2d.png

5. Frequent shortcuts

5.1 Direct shortcut

  • Ctrl + Shift + X:Open plugin window
  • Ctrl + Shift + P:Open command window
  • Ctrl + Shift + D:Open debug window

5.2 Shortcut plugins

  • Add tag for struct fields
Figure 9:Autofill struct fields
8e0b532f001e
  • auto fill struct fields

    • go get -u github.com/davidrjenni/reftools/cmd/fillstruct

About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK