如何用VSCode 代替 Goland
我是从Java转Golang的. 所以IDE第一时间使用的Goland. 对我来说使用Goland更顺滑. 但Goland也有些不足. 比如内存的占用/索引构建的耗时等. 每次听见笔记本嗡嗡的声音, 就不由烦躁. 所以近期就决定换VSCode.
VSCode是我经常用的文本编辑器, 得益于它的插件化. 功能异常强大, 尤其在Go插件从微软转到Go官方后, 性能更加强大. 所以Goland上经常用的功能, VSCode基本都有. 所以为替换Goland奠定了基础.
预备知识
VSCode官网有几篇教程值的看下, 并实操一下, 会加深对VSCode操作的理解.
插件
美化相关
- Dracula Official: 主题插件
- Material Icon Theme: 图标
- vscode-icons: 图标
开发语言相关
- C/C++
- Python
编程辅助
通用
- GitLens — Git supercharged
- Code Spell Checker: 单词拼写检测
- Comment Translate: 注释翻译
- Hex Editor
- Markdown All in One
- Polacode
- Project Manager
- Remote - SSH
- TODO Highlight
- Todo Tree
- TOML Language Support
- Vim
- Visual Studio IntelliCode
- YAML
- Paste JSON as Code:
- Bookmarks:
Golang
- Go / Go Nightly: 二选一
- Go Test Explorer
- Docker:
- Kubernetes:
前端
-
Auto Close Tag: html/xml适用
-
Auto Rename Tag: html/xml适用
-
Bracket Pair Colorizer: 用不同颜色配对不同层级的括号
-
indent-rainbow: 彩虹色缩进
-
filesize: 文件大小
-
Path Intellisense: 路径自动补全
-
npm Intellisense: npm包自动补全
-
Sort lines: 多行排序
-
Prettier: Code formatter:
-
Open in Browser:
-
JavaScript and TypeScript Nightly
-
ESLint
-
NPM
-
Debugger for Chrome
-
Vue
- Vetur
- Vue VSCode Snippets
- Vue.js Extension Pack :Vue 开发插件集
- Vue 2 Snippets :Vue 代码片段
-
React
- ES7 React/Redux/GraphQL/React-Native snippets
- Umi Pro
设置
{
// vscode 基础设置 // 已新窗口打开 "window.openFoldersInNewWindow": "on", // 每次已最大化打开 "window.newWindowDimensions": "maximized", "workbench.startupEditor": "none",
// UI美化 // 图标 "workbench.iconTheme": "material-icon-theme", // "workbench.iconTheme": "vscode-icons", // 主题 "workbench.colorTheme": "Dracula", "editor.fontSize": 14, "editor.fontFamily": "Cascadia Code, Source Code Pro, Menlo, Monaco, 'Courier New', monospace", "editor.lineHeight": 20,
// 编辑器设置相关 // 终端字体设置 "terminal.integrated.fontFamily": "Cascadia Code, Hack Nerd Font", // 控制在显示建议列表时如何预选建议。 "editor.suggestSelection": "first", // 显示空白字符space/tab "editor.renderWhitespace": "all", // 显示不可见字符 "editor.renderControlCharacters": true, // 关闭VSCode的默认单击预览文件模式, 而是双击打开文件 "workbench.list.openMode": "doubleClick", // 是否已紧凑格式展示: 默认为true, 空文件夹合并到上一级目录中 "explorer.compactFolders": false, // 文件自动保存 "files.autoSave": "onFocusChange", // 按下Tab键是否自动填入最佳推荐值 "editor.tabCompletion": "on", "editor.codeActionsOnSave": { // 这个属性能够在保存时,自动调整 import 语句相关顺序,能够让你的 import 语句按照字母顺序进行排列 "source.organizeImports": true }, // 显示行号 "editor.lineNumbers": "on", // 自动折行 "editor.wordWrap": "on", // 编辑器可以按屏幕宽度自动换行 "editor.accessibilitySupport": "off", // 关闭mini地图 "editor.minimap.enabled": false,
// golang配置 "go.toolsManagement.autoUpdate": true, "go.inferGopath": false, // "go.buildOnSave": "workspace", "go.lintOnSave": "package", "go.vetOnSave": "package", "go.buildTags": "", "go.buildFlags": [], "go.lintFlags": [], "go.vetFlags": [], "go.coverOnSave": false, "go.useCodeSnippetsOnFunctionSuggest": false, "go.formatTool": "goreturns", "go.gocodeAutoBuild": false, "go.useLanguageServer": true, // gopls 启用 "go.alternateTools": { "go-langserver": "gopls", }, "go.languageServerExperimentalFeatures": { "format": true, "autoComplete": true }, "go.testFlags": [ "-v" ], "go.docsTool": "gogetdoc", "go.lintTool": "golangci-lint", "[go]": { "editor.codeActionsOnSave": { "source.organizeImports": true }, "editor.formatOnSave": true, // Optional: Disable snippets, as they conflict with completion ranking. "editor.snippetSuggestions": "none", }, "[go.mod]": { "editor.formatOnSave": true, "editor.codeActionsOnSave": { "source.organizeImports": true, }, }, // 这用于在 Output 中输出客户端和 gopls Server 的通讯,方便调试,根据需要开启。 "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, // "completeUnimported": true }, "go.languageServerFlags": [ "-remote=auto", "-logfile=auto", "-debug=:0", "-rpc.trace", ],
}快捷键(MacOS)
- 快捷键显示列表: CMD + K, S
代码导航
基础快捷键
- 命令面板(The Command palette): CMD+Shift+p
- 打开关闭侧边栏(Opening and closing the sidebar): CMD+b
- 文件浏览器(File Explorer): CMD + Shift + e, 也可以此命令在Explorer与Editer Window之间跳转
- 插件市场(Marketplace): CMD + Shift + x,
- 切换工作区间(Switching workspaces): Ctrl + r, 注意: 此操作必须在文件Explorer窗口.
- 打开终端窗口: Ctrl + `
- 跳转文件: CMD+P
- 跳转行: CMD+P界面, 输入:, 在输入行号
- 符号跳转(Go to Symbol): Shift+CMD+O(字母O), 可以在当前页面方法/变量/struct等快速跳转
- 选择一个单词: CMD+d, 按多次可选中多个相同的单词
- 关闭当前Window: CMD+W
- 关闭所有已打kdWindow: CMD+K, W
窗口相关
- 窗口切分: CMD+Shift+P 输入: split
- 切分窗口切换: CMD+数字
- 切分方向置换: CMD+Option+0(数字0)
代码相关
- 代码折叠/展开: CMD+Option+[或]
- Format文件: Shift+Option+F
- 编辑器打开的文件列表: Ctrl + Tab
- 上一个/下一个文件之间跳转: Ctrl + -, Ctrl + Shift + -
- 显示调用层级(Show Call Hierarchy): Option + Shift + H
- 当前项目找到所有引用(Find All References): Option + Shift + F12
- 跳转定义(Go to Definition): F12
- 跳转实现(Go to Implementations): CMD+F12
- 跳转引用(Go to References): Shift+F12
- 跳转类型定义(Go to Type Definition): 无快捷键, 需要自己绑定, 或通过CMD+P后输入指令
- 在当前窗口已小视窗查看定义(Peek Definition): Option+F12, 无需页面之间跳转.
- 遍历当前文件错误/警告信息: F8/Shift+F8(下一个/上一个)
- 生成当前函数对应的测试函数: 无快捷键, 可通过CMD+P, 输入指令 Go: Generate Unit Test For Function
- 添加/取消书签: CMD+Option+K
重构(Refactor)
- 重命名: F2, 可重命名变量/方法等
- 提取方法: CMD+.
- 当前行上下移动: Option + 上方向/下方向
调试
-
添加断点/取消当前光标行断点: F9
-
已Debug Run运行: F5, 注意: Test()或main()都可以.
- 下一步: F10
- 跳进函数: F11
- 跳出函数: Shift + F11
-
停止Debug(Stop Debug Run): Shift+F5
-
重启Debug(Restart Debug Run main()): Shift+CMD+F5
-
自定义快捷键(CMD+k, s):
- 运行当前光标所在的测试函数(go.test.cursor): Option + t
- 跳转当前函数的测试函数(go.toggle.test.file): Option + Shift + t
- 生成对应测试文件(go.test.generate.file): 无
- 生成测试函数(go.test.generate.function): 无
参考资料
#01 Blogs/blog# #110 IT/vscode#