go-vscode-windows安装部署

一、安装

安装Go安装包:https://golang.org/dl/
根据系统下载相应的安装包

 

二、配置环境变量

 

需要配置两个环境变量GOROOT和GOPATH,前者为安装目录,后者为开发目录
同时将%GOROOT%\bin和%GOPATH%\bin放在环境变量中

 

三、安装VSCODE并配置环境

1、添加插件

在vscode的扩展中输入Go,安装第一个插件

 

2、目录创建

用vscode打开%GOPATH%目录,在该目录下创建如下目录:
src\github.com\golang\tools
src\github.org\x

创建代码目录以及文件hello.go

demo\hello.go

package main

import "fmt"

func main() {
    fmt.Printf("Hello World!")
}

关闭vscode

3.设置go环境

启动命令行,根据系统环境输入设置go环境的两条命令:
---------------------------
Go 1.13 及以上(推荐)

打开你的终端并执行

$ go env -w GO111MODULE=on
$ go env -w GOPROXY=https://goproxy.cn,direct
完成。

macOS 或 Linux

打开你的终端并执行

$ export GO111MODULE=on
$ export GOPROXY=https://goproxy.cn
或者

$ echo "export GO111MODULE=on" >> ~/.profile
$ echo "export GOPROXY=https://goproxy.cn" >> ~/.profile
$ source ~/.profile
完成。

---------------------------

4.更新Go的Tools

再次打开vscode
按下Ctrl+Shift+P,选择Go: Install/Update Tools
右下角弹框,选择All Install,如下所示:
---------------------------
gocode
gopkgs
go-outline
go-symbols
guru
gorename
gotests
gomodifytags
impl
fillstruct
goplay
godoctor
dlv
gocode-gomod
godef
goreturns
golint

Installing github.com/mdempsky/gocode SUCCESS
Installing github.com/uudashr/gopkgs/v2/cmd/gopkgs SUCCESS
Installing github.com/ramya-rao-a/go-outline SUCCESS
Installing github.com/acroca/go-symbols SUCCESS
Installing golang.org/x/tools/cmd/guru SUCCESS
Installing golang.org/x/tools/cmd/gorename SUCCESS
Installing github.com/cweill/gotests/... SUCCESS
Installing github.com/fatih/gomodifytags SUCCESS
Installing github.com/josharian/impl FAILED
Installing github.com/davidrjenni/reftools/cmd/fillstruct SUCCESS
Installing github.com/haya14busa/goplay/cmd/goplay SUCCESS
Installing github.com/godoctor/godoctor SUCCESS
# Installing github.com/go-delve/delve/cmd/dlv SUCCESS # 为Go调试器,若是没有请看下一个步骤
Installing github.com/stamblerre/gocode SUCCESS
Installing github.com/rogpeppe/godef SUCCESS
Installing github.com/sqs/goreturns SUCCESS
Installing golang.org/x/lint/golint SUCCESS
---------------------------

 

将更新成功之后的src\github.com\golang\tools文件夹复制到src\github.org\x\tools下

 

5.debug环境配置

下载github资源:https://github.com/go-delve/delve
git命令如下:git clone https://github.com/go-delve/delve.git
更新完成后目录更改到:%GOPATH%\src\github.com\go-delve\delve
然后命令行运行:go get github.com/go-delve/delve/cmd/dlv

若是没有响应,就是安装上了dlv调试工具

6.配置debug调试信息

vscode中点击左侧debug按钮,创建launch.json,如下:

{
// Use IntelliSense to learn about possible attributes.
// Hover to view descriptions of existing attributes.
// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
"version": "0.2.0",
"configurations": [
{
"name": "Launch",
"type": "go",
"request": "launch",
"mode": "auto",
// "host": "127.0.0.1",
// "port": 2345,
// "program": "${fileDirname}",
"program": "D:\\GoPath\\demo\\hello.go",
// "env": {"go.gopath": "D:\\GoPath"}, 
// "env": {},
"args": [],
// "showLog": true
}
]
}

 

其中program指明当前需要调试的go文件

 

命令行运行 go env -w GO111MODULE=off

7.生效

重启vscode,就可以调试啦

 

 

参考 

1.vs code golang开发 代理设置, 告别手动下载工具包

2.Debugging

3.vscode运行程序时报go: cannot find main module, but found Gopkg.lock

4.Windows下Go安装&环境配置&编译运行

5.VSCode下,项识别为 cmdlet、函数、脚本文件或可运行程序的名称

6.VSCode Go环境搭建 (Windows版)

7.Ubuntu环境下安装VSCode以及Golang语言环境

8.语法学习

posted @ 2020-07-10 13:32  绍荣  阅读(277)  评论(0编辑  收藏  举报