VS Code 中添加 Snippet 提高 Golang 编辑效率

版权声明:本文为aggresss原创文章,未经博主允许不得转载。 作者:[email protected] https://blog.csdn.net/aggresss/article/details/79791291

VS Code 中添加 Snippet 可以提高 Coding 的效率,降低实践成本,下面是我录制的一段屏幕演示:
vs_code_snippet

步骤如下:

  1. 使用 Shift + Ctrol + P 调出命令窗口,输入: snippets
  2. 选择 Preferences: Open User Snippets
  3. 选择需要添加 snippet 的语言,以输入 go 为例,回车后就可以编辑 go.json 文件,在 go.json 文件中添加snippet 即可,我这里写了一个 Helloworld:
"package main": {
        "prefix": "main",
        "body":[
            "package main\n",
            "import (",
            "\t\"fmt\"",
            "\t$1",
            ")\n",
            "func main() {",
            "\t$2",
            "}",
        ],
        "description": "short for package main"
    }

保存后回到代码编辑区,使用 prefix 中声明的关键字就可以调出预先编辑好的 snippet.

以下语法规则需要掌握:

  1. 换行使用 \n 表示, Tab 使用 \t 表示;
  2. 双引号使用 \” 表示;
  3. 变量 $1 $2 表示预留代码编辑位置,snippet 调出成功后,使用 Tab 键 可以将光标从 $0 依次跳转;

VS Code snippets 配置文件默认保存位置
Linux : ${HOME}/.config/Code/User/snippets
Windows : %appdata%/Code/User/snippets


参考文档

  1. Creating your own snippets
  2. How to add custom code snippets in VSCode?
  3. Share snippets with your team in VS Code

End

猜你喜欢

转载自blog.csdn.net/aggresss/article/details/79791291