# vscode Golang 开发环境配置 ## 1.安装Golang * 下载安装包,国内无法直接访问google,国内站点下载 `https://studygolang.com/dl` * centos7.2 安装 Golang ``` [xxx@centos ~]$ wget https://studygolang.com/dl/golang/go1.12.5.linux-amd64.tar.gz [xxx@centos ~]$ sudo tar -xzvf Download/go1.12.5.linux-amd64.tar.gz -C /usr/local ``` * 配置GOPATH 1. linux上的默认GOPATH为`$HOME/go`,即`/home/xxx/go`,编辑`.bashrc`文件增加以下内容 ``` [xxx@centos ~]$ sudo gedit ./.bashrc export GOROOT=/usr/local/go export GOPATH=/home/xxx/go export PATH=$PATH:$GOROOT/bin:$GOPATH/bin [xxx@centos ~]$ source .bashrc ``` 2. windows上默认GOPATH为`%USERPROFILE%\go`,即`C:\Users\Administrator\go`, 建立C:\Users\Administrator\go\src文件夹,将其作为工作目录,而不用设置GOPATH ## 2.安装vscode * 添加源安装软件 ``` [xxx@centos ~]$ sudo rpm --import https://packages.microsoft.com/keys/microsoft.asc [xxx@centos ~]$ sudo sh -c 'echo -e "[code]\nname=Visual Studio Code\nbaseurl=https://packages.microsoft.com/yumrepos/vscode\nenabled=1\ngpgcheck=1\ngpgkey=https://packages.microsoft.com/keys/microsoft.asc" > /etc/yum.repos.d/vscode.repo' [xxx@centos ~]$ sudo yum check-update [xxx@centos ~]$ sudo yum install code ``` * 在扩展搜索并安装vscode的Go组件 * 打开工作目录,创建go文件开始编辑代码 `File`->`Open Floder`->`/home/xxx/go/src/` ## 3.开发环境配置 * 安装Golang插件,可以提供代码提示、格式化等功能提高编程效率,如 gocode、gocode-gomod、godef、golint、go-outline、gopkgs、gorename、goreturns、go-symbols、guru等。 但是国内访问golang下载tools包失败,导致部分插件不能安装。 * 可以从github下载源码 ``` [xxx@centos src]$ git clone https://github.com/golang/tools.git 解压到 /home/xxx/go/src/golang.org/x/tools [xxx@centos src]$ git clone https://github.com/golang/lint.git 解压到 /home/xxx/go/src/golang.org/x/lint ``` * 自行编译在go/bin生成可执行文件 ``` [xxx@centos src]$ go build -o ../bin/gocode github.com/mdempsky/gocode/ [xxx@centos src]$ go build -o ../bin/go-outline github.com/ramya-rao-a/go-outline/ [xxx@centos src]$ go build -o ../bin/go-symbols github.com/acroca/go-symbols/ [xxx@centos src]$ go build -o ../bin/guru golang.org/x/tools/cmd/guru/ [xxx@centos src]$ go build -o ../bin/gorename golang.org/x/tools/cmd/gorename/ [xxx@centos src]$ go build -o ../bin/golint golang.org/x/lint/golint/ [xxx@centos src]$ go build -o ../bin/gocode-gomod github.com/stamblerre/gocode/ [xxx@centos src]$ go build -o ../bin/goreturns github.com/sqs/goreturns/ ```