因为不花钱的goland毕竟不是正途,所以耗点精力搭建基于vscode的开发环境是有必要的。

跟着本文,你可以得到

  1. docker开发环境
  2. vscode + ssh
  3. 远程golang语言服务器

docker

  • 注意事项: 默认docker不支持fork,所以vscode F5 debug会报错。
--security-opt=seccomp:unconfined
docker run --security-opt=seccomp:unconfined --name wmw-ubuntu-18.04 -p 20022:22 -it ubuntu /bin/bash

安装go

.bashrc
export GOROOT=/usr/local/go
export GOPATH=$HOME/go
export GOBIN=$GOPATH/bin
# 强制开启module模式
# export GO111MODULE=on
# 配置Proxy之后,就可以愉快的go get了
# export GOPROXY=https://goproxy.io
# go1.13+换用go env -w 方式设置,加下文。

export PATH=$GOPATH:$GOBIN:$GOROOT/bin:$PATH:$HOME/.local/bin

注:七牛网,真好人啊。https://goproxy.cn比https://goproxy.io更稳定,速度更快。

$ go env -w GO111MODULE=on
$ go env -w GOPROXY=https://goproxy.cn,direct
GO111MODULE
import写法

vscode

安装插件

Local安装

Name: Remote - SSH
Id: ms-vscode-remote.remote-ssh
Description: Open any folder on a remote machine using SSH and take advantage of VS Code's full feature set.
Version: 0.47.2
Publisher: Microsoft
VS Marketplace Link: https://marketplace.visualstudio.com/items?itemName=ms-vscode-remote.remote-ssh

Name: Remote - SSH: Editing Configuration Files
Id: ms-vscode-remote.remote-ssh-edit
Description: Edit SSH configuration files
Version: 0.47.2
Publisher: Microsoft
VS Marketplace Link: https://marketplace.visualstudio.com/items?itemName=ms-vscode-remote.remote-ssh-edit

Remote安装

Name: Go
Id: ms-vscode.go
Description: Rich Go language support for Visual Studio Code
Version: 0.11.9
Publisher: Microsoft
VS Marketplace Link: https://marketplace.visualstudio.com/items?itemName=ms-vscode.Go

大功告成

Make things better

  • 编辑代码生成
ctrl + shift + p
添加代码生成片段