一些重复写的代码我们可以通过代码片段的方式节省时间。

在VSCode的菜单栏中找到, 然后找到想要创建的语言的代码片段,我这里创建了一个Go语言的代码片段。

File-> Preferences-> User Snippets

虽然Go中已经存在很多代码片段了,比如pkgm, fmtpf, fmtpl, fmtpt, 这些经常用的都已经给到了。 如果自己有需要,只需要根据所给注释添加即可

{
	// Place your snippets for go here. Each snippet is defined under a snippet name and has a prefix, body and 
	// description. The prefix is what is used to trigger the snippet and the body will be expanded and inserted. Possible variables are:
	// $1, $2 for tab stops, $0 for the final cursor position, and ${1:label}, ${2:another} for placeholders. Placeholders with the 
	// same ids are connected.
	// Example:
	// "Print to console": {
	// 	"prefix": "log",
	// 	"body": [
	// 		"console.log('$1');",
	// 		"$2"
	// 	],
	// 	"description": "Log output to console"
	// }
	"go-main-template": {
		"prefix": "main",
		"body": [
			"package main",
			"",
			"func main() {",
			"$0",
			"}",
			""
		],
		"description": "a template of go main function"
	}
}