我们团队大部分的人都是使用mac开发, 而我比较持家舍不得花大价钱来更新换代我5年前买的macbook, 所以我长期使用公司配备的Window10进行日常的开发. 在之前的99%的情况下我的windows10都可以满足安装各种环境(ruby/jekyll,python,php,java). 直到有一个同事在项目中添加confluent-kafka-go来使用消息队列, 发现confluent-kafka-go依赖的是c++, 同时还依赖一个可执行文件,这个可执行文件只支持mac/linux windows10不支持. 更糟糕的是, 我之前觉得windows10子系统用的少, 我也把我的ubuntu子系统删掉了, 同时把hyper-V卸载了, 现在在安装ubuntu子系统就会报错.

程序员绝不说不
  1. Golang->SFTP 自动同步代码到linux开发服务器(SMBA也可以)

  2. linux 服务器: go build -> dvl 开启远程调式服务

  3. Goland 连接dvl 服务地址

1. 远程调试Debug优点

  1. 极大的加快开发速度,减少给在代码中使用大量log来定位错误.

  2. 最大程度的使用linux远程服务器环境, 极大的简化本地部署模拟服务器环境

  3. 可以绕过数据库内网的限制

  4. 不用依赖日志来定位错误(开发效率太低)

  5. 完美的解决一些不支持windows开发的依赖

2. Go远程调试Debug

2.1 linux 安装Golang环境

参考安装golang对应的文章

2.2 linux服务器安装Delve

[root@centos-170-22 ~]#go install github.com/go-delve/delve/cmd/dlv@latest
[root@centos-170-22 ~]#echo $GOPATH/bin
/data/gopath/bin

将此目录添加到path路径

[root@centos-170-22 ~]#cat > /etc/profile.d/dlv.sh<< EOF
PATH=/data/gopath/bin:\$PATH
export PATH
EOF

[root@centos-170-22 ~]#source /etc/profile.d/dlv.sh

2.2 Goland SFTP 自动同步代码

如果找不到SFTP,请安装SFTP Plugin, Goland 添加SFTP: Goland菜单: setting – build – deployment 加号

Goland SFTP 本地代码目录映射

勾选自动上传Goland菜单: Tools -> Deployment -> Auto Upload(Always)

2.3 服务器开启Delve 服务

cd ~/go_project; # golang 代码目录
go build -o app; # 编译golang代码
dlv --listen=:2345 --headless=true --api-version=2 exec ~/go_project/app api; # 开启delve服务  其中 api 是golang程序的参数
~/.bashrc
export GOPROXY=https://goproxy.io
export GO111MODULE=on
alias debugm="cd ~/go_project; go build -o app; dlv --listen=:2345 --headless=true --api-version=2 exec ~/go_project/app api"

2.4 Goland Debug 配置

填写linux 服务的ip 和 delve的端口,自己设置防火墙.

2.5 Goland打断点

每次代码更新的适合需要在linux中 ctrl+c, 在执行debugm(自定义命令) linux 服务器中:

[eric@mojotv.cn ~]$ cat ~/.bashrc #查看当前用户的profile 配置
[eric@mojotv.cn ~]$ debugm # 执行自定义的命令
API server listening at: [::]:2345

在Goland中点击debug按钮

[mojotv.cn@p28537v yzt]$ debugm
API server listening at: [::]:2345
# Golang 点击虫子按钮之后打印出来的
2019/11/27 19:09:29 访问 http://127.0.0.1:9527/swagger-doc for RESTful APIs Swagger 文档
[GIN-debug] [WARNING] Running in "debug" mode. Switch to "release" mode in production.
 - using env:	export GIN_MODE=release
 - using code:	gin.SetMode(gin.ReleaseMode)

[GIN-debug] GET    /swagger-doc/*filepath    --> github.com/gin-gonic/gin.(*RouterGroup).createStaticHandler.func1 (3 handlers)
[GIN-debug] HEAD   /swagger-doc/*filepath    --> github.com/gin-gonic/gin.(*RouterGroup).createStaticHandler.func1 (3 handlers)
INFO[0000] start asset sync runner...                   
[GIN-debug] GET    /screen-shot/assets/:id   --> marvel/handler.AssetScreenShot (4 handlers)
[GIN-debug] GET    /api/v1/admin-division    --> marvel/handler.AdminDivision (4 handlers)
...
[GIN-debug] Listening and serving HTTP on 0.0.0.0:9527

Goland断点效果