众所周知,一些特定的编程语言可能需要Linux环境的支持,比如在golang代码中引用C语言,就需要使用到cgo,而这个环境配置需要Linux作为支撑。如果本机使用的是Windows环境,想要在Linux环境中进行代码编译、调试、部署,还有如下几种方式:
1. 搭建Windows + Linux双系统。Windows进行文档处理等办公操作,Linux进行开发、编译、调试等工作。缺点是,系统切换比较慢,有些沟通工具支持不好,比如在开发环境下,如果团队使用微信进行沟通,就可能导致信息延迟、沟通不畅等问题,从而影响开发效率。 2. 在虚拟机上安装Ubuntu或者Centos等Linux桌面系统。缺点是,虚拟机比较笨重,再在此基础上构建体量更加大的Linux系统,一方面对软硬件环境配置要求比较高,另一方面像一些数据库、接口调试等可视化工具使用不方面。 3. 使用VMware + VScode + Remote SSH远程开发工具 + Ubuntu Server无桌面版。虽然这种方式也使用了虚拟机 + Linux,但是相对使用桌面版系统,会节省不少空间。使用Remote SSH构建远程开发环境
在VMware上安装完Ubuntu Server版后,进入VScode左侧扩展局域栏的Extension扩展市场,搜索Remote - SSH,然后进行Install安装。然后,使用ssh-keygen命令生成密钥对,用public key生成authorized_keys,再进入密钥所在目录cd ~/.ssh,使用scp命令将本地Windows生成的公钥上传到Ubuntu系统,通过如下命令生成authorized keys。
# 本地Windows系统生成公钥、私钥 ssh-keygen cd C:Usersxxx.ssh # 使用scp命令将公钥上传远程Ubuntu的~/.ssh目录下 scp .id_rsa.pub xxx@192.168.xx.xxx:~/.ssh/ # 使用公钥生成authorized keys cat id_rsa.pub >> authorized_keys
然后,选择Remote SSH配置文件,一般选择C:Usersxxx.sshconfig,配置如下字段信息
Host name-of-ssh-host-here
User your-user-name-on-host
HostName host-fqdn-or-ip-goes-here
Port port-of-host
IdentityFile C:Usersxxx.sshid_rsa # 私钥所在路径
配置完上面的Remote SSH后,打开VScode使用Remote SSH,选择远程Ubuntu上的项目文件,即可以进行远程代码开发。
使用VScode通过Remote SSH进行远程开发时,本地开启VScode,连接上remote ssh服务器后,会在远程服务器上生产VScode服务器版本(在~/.vscode-server目录下,该目录下有VScode的二进制文件,用户及remote ssh配置文件settings.json,还有相关扩展extensions)。
Ctrl + Shift + P进行golang扩展组件的Update Install后,配置完Remote SSH开发环境,打开Ubuntu系统上的golang项目后,使用VScode查看代码,使用Ctrl + 鼠标左键进入代码时跳转很慢,查阅资料说,把语言服务器和delay延迟参数设置取消掉,发现仍然解决不了问题,最后折腾半天,在VScode插件市场重新卸载Remote SSH和golang插件,重新安装后,还是跳转缓慢,然后重置之前取消的语言服务器不勾选,即在settings.json中配置"go.useLanguageServer": true,参数为true,开启语言服务器后,跳转正常了,其完成配置如下:
{
"remote.SSH.remotePlatform": {
"xxx.xxx.xx.xxx": "linux",
"Ubuntu": "linux",
"VM-Ubuntu": "linux"
},
"remote.SSH.showLoginTerminal": true,
"editor.fontSize": 19,
"extensions.ignoreRecommendations": true,
"go.formatTool": "goimports",
"editor.hover.delay": 30,
// The default for files.watcherExclude excludes node_modules and some folders under .git, but you can add other directories that you don't want VS Code to track.
"files.watcherExclude": {
"**/.git/objects.git/subtree-cachenode_modules.hg/store/**": true
},
"go.goroot": "/usr/local/go",
"go.gopath": "/home/xxx/ws/go",
//第三方库代码提示
"go.inferGopath": true,
//自动完成未导入的包
"go.autocompleteUnimportedPackages": true,
"go.gocodePackageLookupMode": "go",
"go.gotoSymbol.includeImports": true,
"go.useCodeSnippetsOnFunctionSuggest": true,
"go.useCodeSnippetsOnFunctionSuggestWithoutType": true,
"[go]": {
"editor.formatOnSave": true,
"editor.codeActionsOnSave": {
"source.organizeImports": true,
}, // Optional: Disable snippets, as they conflict with completion ranking. "editor.snippetSuggestions": "none",
},
"[go.mod]": {
"editor.formatOnSave": true,
"editor.codeActionsOnSave": {
"source.organizeImports": true,
},
},
"go.trace.server": "verbose",
"gopls": {
// Add parameter placeholders when completing a function.
"usePlaceholders": false,
// If true, enable additional analyses with staticcheck.
// Warning: This will significantly increase memory usage. "staticcheck": false,
},
"go.languageServerFlags": [
"-remote=auto", "-logfile=auto", "-debug=:0", "-rpc.trace",
],
"go.languageServerExperimentalFeatures": {},
"tabnine.experimentalAutoImports": true,
}
PS:使用remote ssh进行远程开发,一定要打开语言服务器选项,要不然代码跳转会非常慢。
参考真香!使用 VSCode 进行远程开发与调试
vscode远程开发配置remote ssh
Win10环境下配置VScode远程开发ssh-remote(免密登录)
配置ssh免密登录_配置vscode 远程开发+ 免密登录
goland远程开发
vscode 的 golang 提示很慢
vscode gopls 跳转太慢解决方法
解决VScode配置远程调试Linux程序的问题



