Here's a quick summary of the snippets I personally use when programming in Go. There are various types, from those that are used very often to those that are used only occasionally. It looks like it can be used! !! !! I would be happy if you could introduce just the ones that you thought!
First of all, I will briefly introduce how to register snippets with vscode
Code -> Preferences -> User Snippets
↓
Then a screen like this will appear, so click go
↓
Then the following json file will be displayed, so it is like adding the snippet you want to use there
Snippet registration can be done with the following syntax
python
"Snippet title": {
"prefix": "What kind of characters should be typed as predictive conversion?",
"body": [
"What kind of character string should be output?",
"If you want to open a business, you can open it by entering it separated by commas like this",
],
"description": "Detailed explanation"
},
You can also decide where to hold the cursor by entering the characters $ 1
after typing in the snippet.
For example, when you create the following snippet
python
"Print to console": {
"prefix": "fff",
"body": ["fmt.Printf(\"==========%#v\\n\", $1)"],
"description": "Log to check the movement"
},
After using the snippet
python
fmt.Printf("==========%#v\n", )
//↑ The cursor comes here
Will come to say
Character you want to insert | Insertion method |
---|---|
$ | \\$ |
tab | \t |
Double quote | " |
Single quote | ' |
--Debug print --Structure function
This is a print for debugging. It is often used to check the contents of the structure and to identify the location of the error.
python
"Print to console": {
"prefix": "fff",
"body": ["fmt.Printf(\"==========%#v\\n\", $1)"],
"description": "Log to check the movement"
},
↓ fff
python
fmt.Printf("==========%#v\n", )
I created it because it's too annoying to enter parentheses when creating a struct method. I personally use this too
python
"func": {
"prefix": "fc",
"body": ["func ($1) $2($3)($4){", "\t$0", "}"],
"description": "Create a struct function"
},
↓fc
python
func () ()(){
}
package
Just print package
It's surprisingly easy!
python
"package": {
"prefix": "pac",
"body": "package",
"description": "I don't even want to write a "package""
},
↓pac
python
package
It creates a template for table-driven testing at once. It's quite annoying to write from 0 every time, so I made it roughly first with this (For table-driven testing, click here](https://qiita.com/takehanKosuke/items/cbfc88c4b7956adede79))
python
"test func": {
"prefix": "fct",
"body": [
"func Test$1(t *testing.T) {",
"\tt.Parallel()",
"\tasserts := assert.New(t)",
"\ttests := []struct{",
"\t\tname string",
"\t\tinput string",
"\t\toutput string",
"\t}{",
"\t\t{",
"\t\t\tname: \"\",",
"\t\t\tinput: \"\",",
"\t\t\toutput: \"\",",
"\t\t},",
"\t}",
"\tfor _, td := range tests {",
"\t\ttd := td",
"\t\tt.Run(fmt.Sprintf(\"$1: %s\", td.name), func(t *testing.T) {",
"\t\t})",
"\t}",
"}"
],
"description": "Table test base"
},
↓fct
python
func Test(t *testing.T) {
t.Parallel()
asserts := assert.New(t)
tests := []struct{
name string
input string
output string
}{
{
name: "",
input: "",
output: "",
},
}
for _, td := range tests {
td := td
t.Run(fmt.Sprintf(": %s", td.name), func(t *testing.T) {
})
}
}
gomock It is a command that you will definitely write in gomock, which is a mock library often used in go. It's useful because it's easy to forget how to draw it.
python
"Gomock template": {
"prefix": "tgomock",
"body": [
"//gomock settings",
"ctrl := gomock.NewController(t)",
"defer ctrl.Finish()"
],
"description": "Create ginContext for test"
},
↓tgomock
python
//gomock settings
ctrl := gomock.NewController(t)
defer ctrl.Finish()
This is a snippet used when writing tests including gin context in gin, which is a framework of go (see here for details).
python
"test gin Context": {
"prefix": "tgincontext",
"body": [
"//Generating a gin context for test",
"ginContext, _ := gin.CreateTestContext(httptest.NewRecorder())",
"req, _ := http.NewRequest(\"GET\", \"/\", nil)",
"//req.Header.Add(\"Authorization\", td.inputHeader)",
"//req.Header.Add(\"Authorization\", td.inputHeader)",
"ginContext.Request = req"
],
"description": "Initial fixed phrase for gomock"
},
↓tgincontext
python
//Generating a gin context for test
ginContext, _ := gin.CreateTestContext(httptest.NewRecorder())
req, _ := http.NewRequest("GET", "/", nil)
//req.Header.Add("Authorization", td.inputHeader)
//req.Header.Add("Authorization", td.inputHeader)
ginContext.Request = req
This is a snippet used when skipping http requests with go. It's easy to forget how to add headers and parameters, so I always remember using this.
python
"HTTP GET request": {
"prefix": "httpget",
"body": [
"url := \"\"",
"req, err := http.NewRequest(\"GET\", url, nil)",
"if err != nil{",
"\treturn nil, err",
"}",
"//header",
"req.Header.Add(\"\", \"\")",
"//Query parameters",
"params := req.URL.Query()",
"params.Add(\"\",\"\")",
"req.URL.RawQuery = params.Encode()"
],
"description": "HTTP GET request"
}
↓httpget
python
url := ""
req, err := http.NewRequest("GET", url, nil)
if err != nil{
return nil, err
}
//header
req.Header.Add("", "")
//Query parameters
params := req.URL.Query()
params.Add("","")
req.URL.RawQuery = params.Encode()
Personally, snippets are fixed phrases, but many of them are made for things that tend to forget detailed setting methods. I think I will use this often in the future! I would like to add more information as needed!
Recommended Posts