-
Notifications
You must be signed in to change notification settings - Fork 2k
/
Copy pathgo.snippets
280 lines (214 loc) · 4.02 KB
/
go.snippets
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
snippet v "shorthand variable declaration"
${1} := ${2}
snippet vr "variable initialization"
var ${1:t} ${0:string}
snippet var "variable declaration"
var ${1} ${2} = ${3}
snippet vars "variables declaration"
var (
${1} ${2} = ${3}
)
snippet ap "append"
append(${1:slice}, ${0:value})
snippet bl "bool"
bool
snippet bt "byte"
byte
snippet br "break"
break
snippet ch "channel"
chan ${0:int}
snippet cs "case"
case ${1:value}:
${0:${VISUAL}}
snippet co "constants with iota"
const (
${1:NAME1} = iota
${0:NAME2}
)
snippet cn "continue"
continue
snippet df "defer"
defer ${0:func}()
snippet dfr "defer recover"
defer func() {
if err := recover(); err != nil {
${0:${VISUAL}}
}
}()
snippet im "import"
import (
"${1:package}"
)
snippet in "interface"
interface{}
snippet inf "full interface "
type ${1:name} interface {
${2:/* methods */}
}
snippet if "if condition"
if $1 {
${2:${VISUAL}}
}
snippet ife "if else condition"
if $1 {
${2:${VISUAL}}
} else {
${0}
}
snippet el "else"
else {
${0:${VISUAL}}
}
snippet ir "if error not nil, return err"
if err != nil {
return err
}
${0}
snippet f "false"
false
snippet ft "fallthrough"
fallthrough
snippet fl "float"
float32
snippet f3 "float32"
float32
snippet f6 "float64"
float64
snippet for "for loop"
for ${1}{
${0:${VISUAL}}
}
snippet fori "for int loop"
for ${2:i} := 0; $2 < ${1:count}; $2${3:++} {
${0:${VISUAL}}
}
snippet forr "for range loop"
for ${1:e} := range ${2:collection} {
${0:${VISUAL}}
}
snippet fun "function"
func ${1:funcName}(${2}) ${3:error} {
${4}
}
${0}
snippet fum "method"
func (${1:receiver} ${2:type}) ${3:funcName}(${4}) ${5:error} {
${6}
}
${0}
snippet fumh "http handler function on receiver"
func (${1:receiver} ${2:type}) ${3:funcName}(${4:w} http.ResponseWriter, ${5:r} *http.Request) {
${0:${VISUAL}}
}
snippet lf "log printf"
log.Printf("%${1:s}", ${2:var})
snippet lp "log println"
log.Println("${1}")
snippet mk "make"
make(${1:[]string}, ${0:0})
snippet mp "map"
map[${1:string}]${0:int}
snippet main "func main()"
func main() {
${1}
}
${0}
snippet nw "new"
new(${0:type})
snippet pa "package"
package ${1:main}
snippet pn "panic"
panic("${0:msg}")
snippet pf "fmt.Printf()"
fmt.Printf("%${1:s}\n", ${2:var})
snippet pl "fmt.Println()"
fmt.Println("${1:s}")
snippet rn "range"
range ${0}
snippet rt "return"
return ${0}
snippet rs "result"
result
snippet sl "select"
select {
case ${1:v1} := <-${2:chan1}
${3}
default:
${0}
}
snippet sr "string"
string
snippet st "struct"
type ${1:name} struct {
${2:/* data */}
}
${0}
snippet sw "switch"
switch ${1:var} {
case ${2:value1}:
${3}
case ${4:value2}:
${5}
default:
${0}
}
snippet ps "fmt.Sprintf"
fmt.Sprintf("%${1:s}", ${2:var})
snippet t "true"
true
snippet g "goroutine named function"
go ${1:funcName}(${0})
snippet ga "goroutine anonymous function"
go func(${1} ${2:type}) {
${3:/* code */}
}(${0})
snippet test "test function"
func Test${1:name}(t *testing.T) {
${0:${VISUAL}}
}
snippet testt "table test function"
func Test${1:name}(t *testing.T) {
tests := []struct {
name string
}{
{
name: "${2:test name}",
},
}
for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
${0:${VISUAL}}
})
}
}
snippet bench "benchmark function"
func Benchmark${1:name}(b *testing.B) {
for i := 0; i < b.N; i++ {
${2}
}
}
${0}
snippet cl "composite literals"
type ${1:name} struct {
${2:attrName} ${3:attrType}
}
snippet om "if key in a map"
if ${1:value}, ok := ${2:map}[${3:key}]; ok == true {
${4:/* code */}
}
snippet gg "Grouped globals with anonymous struct"
var ${1:var} = struct{
${2:name} ${3:type}
}{
$2: ${4:value},
}
snippet ja "Marshalable json alias"
type ${1:parentType}Alias $1
func (p *$1) MarshalJSON() ([]byte, error) {
return json.Marshal(&struct{ *$1Alias }{(*$1Alias)(p)})
}
snippet errwr "Error handling with fmt.Errorf"
if ${1}err != nil {
return fmt.Errorf("${2} %w", err)
}