编译与调试
调试时从终端键盘输入
调试带有需要用户键盘输入的程序时,VSCode报错:Unable to process `evaluate`: debuggee is running,因为调试器不知道具体是哪个终端输入。需要配置启动文件 .vscode/launch.json 类似如下(注意 console 行): 参考VSCode运行Go程序报错:Unable to process `evaluate`: debuggee is running-CSDN博客
{"version": "0.2.0","configurations": [{"name": "my-stock-history","type": "go","request": "launch","mode": "auto","program": "main.go","console": "integratedTerminal"}]
}
golang获取键盘输入的方式有好几种,可参考 go语言里面怎么获取键盘录入 • Worktile社区
Linux → Windows带cgo交叉编译
开发环境 Linux (Deepin 20.9),目标环境 Windows 10 (LTSC 21H2)。使用了 Sqlite3 不仅要求两个环境安装了Sqlite3的命令工具,而且 Sqlite3 使用了 CGO,所以不得不在开发环境安装目标环境的C/C++编译器:
sudo apt-get install gcc-mingw-w64
然后,编译命令前需要指定环境变量参数:目标操作系统名称、架构;启用CGO,使用的C/C++编译工具(不指定CC可能遇到错误提示 gcc: error: unrecognized command line option ‘-mthreads’; did you mean ‘-pthread’?)。
GOOS=windows GOARCH=amd64 CGO_ENABLED=1 CC=x86_64-w64-mingw32-gcc go build -o my-app.exe
有关交叉编译细节,可参考 https://juejin.cn/post/7506843286654582835
没有使用CGO的程序,不要启用CGO!