/
go.snippets
317 lines (304 loc) · 5.26 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
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
# import snippets from vim-go https://github.com/fatih/vim-go
#
# shorthand variable declaration
snippet :
${1} := ${0}
# anonymous function
snippet anon
${1:fn} := func() {
${0}
}
# append
snippet ap
append(${1:slice}, ${0:value})
# append assign
snippet ap=
${1:slice} = append($1, ${0:value})
# break
snippet br
break
# channel
snippet ch
chan ${0:int}
# case
snippet case
case ${1:value}:
${0}
# constant
snippet con
const ${1:NAME} ${2:Type} = ${0:0}
# constants
snippet cons
const (
${1:NAME} ${2:Type} = ${3:value}
${0}
)
# constants with iota
snippet iota
const (
${1:NAME} ${2:Type} = iota
${0}
)
# continue
snippet cn
continue
# default case
snippet default
default:
${0}
# defer
snippet df
defer ${1:func}(${2})
${0}
snippet def
defer func() {
${0}
}()
# defer recover
snippet defr
defer func() {
if err := recover(); err != nil {
${0}
}
}()
# gpl
snippet gpl
/*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, see <http://www.gnu.org/licenses/>.
*
* Copyright (C) ${1:Author}, `strftime("%Y")`
*/
${0}
# import
snippet import
import (
"${1:package}"
)
# full interface snippet
snippet interface
type ${1:Interface} interface {
${2:/* TODO: add methods */}
}
# if condition
snippet if
if ${1:condition} {
${0}
}
# else snippet
snippet else
else {
${0}
}
# error snippet
snippet errn
if err != nil {
return err
}
${0}
# error snippet in TestFunc
snippet errt
if err != nil {
t.Fatal(err)
}
# error snippet in TestFunc
snippet errtf
if err != nil {
t.Fatalf("${0} failed: %s", err)
}
# error snippet with two return values
snippet errn,
if err != nil {
return ${1:nil}, err
}
${0}
# error snippet handle and return
snippet errh
if err != nil {
${1}
return
}
${0}
# error snippet with panic
snippet errp
if err != nil {
panic(${1})
}
${0}
# json snippet
snippet json
\`json:"${1:keyName}"\`
# yaml snippet
snippet yaml
\`yaml:"${1:keyName}"\`
# fallthrough
snippet ft
fallthrough
# for loop
snippet for
for ${1} {
${0}
}
# for integer loop
snippet fori
for ${1:i} := 0; $1 < ${2:N}; $1++ {
${0}
}
# for range loop
snippet forr
for ${2:k}, ${3:v} := range ${1} {
${0}
}
# function
snippet func
func ${1:function}(${2}) ${3:error }{
${0}
}
# Fmt Printf debug
snippet ff
fmt.Printf("${1} = %+v\n", $1)
${0}
# Fmt Println debug
snippet fn
fmt.Println("${1}")
# log printf
snippet lf
log.Printf("${1} = %+v\n", $1)
# log println
snippet ln
log.Println("${1}")
# make
snippet make
make(${1:[]string}, ${2:0})${0}
# map
snippet map
map[${1:string}]${0:int}
# main()
snippet main
func main() {
${0}
}
# method
snippet meth
func (${1:self} ${2:Type}) ${3:Do}(${4}) ${5:error }{
${0}
}
# ok
snippet ok
if !ok {
${0}
}
# package
snippet package
// Package $1 provides ${2:...}
package ${1:main}
${0}
# panic
snippet panic
alias pn
panic("${0}")
# return
snippet return
alias rt
return ${0}
# select
snippet select
select {
case ${1:v1} := <-${2:chan1}
${0}
}
# struct
snippet st
type ${1:Type} struct {
${0}
}
# switch
snippet switch
switch ${1:var} {
case ${2:value1}:
${0}
}
# sprintf
snippet sp
fmt.Sprintf("%${1:s}", ${2:var})
# goroutine named function
snippet go
go ${1:funcName}(${0})
# goroutine anonymous function
snippet gof
go func(${1}) {
${3:/* TODO */}
}(${2})
# test function
snippet test
func Test${1:Function}(t *testing.T) {
${0}
}
# test server
snippet tsrv
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
fmt.Fprintln(w, ${1:`response`})
}))
defer ts.Close()
//Use testing server url (type string) somewhere
${0:someUrl} = ts.URL
# test error
snippet ter
if err != nil {
t.Errorf("${1}")
}
# test fatal error
snippet terf
if err != nil {
t.Fatalf("${1}")
}
# test example
snippet example
func Example${1:Method}() {
${0}
// Output:
}
# test benchmark
snippet benchmark
func Benchmark${1:Method}(b *testing.B) {
for i := 0; i < b.N; i++ {
${0}
}
}
# variable declaration
snippet var
var ${1:x} ${2:Type}${3: = ${0:value\}}
# variables declaration
snippet vars
var (
${1:x} ${2:Type}${3: = ${0:value\}}
)
# equals fails the test if exp is not equal to act.
snippet eq
if !reflect.DeepEqual(${1:expected}, ${2:actual}) {
_, file, line, _ := runtime.Caller(0)
fmt.Printf("%s:%d:\n\n\texp: %#v\n\n\tgot: %#v\n\n", filepath.Base(file), line, $1, $2)
t.FailNow()
}
snippet hf
func ${1:handler}(w http.ResponseWriter, r *http.Request) {
${0:fmt.Fprintf(w, "hello world")}
}
snippet hhf
${1:http}.HandleFunc("${2:/}", func(w http.ResponseWriter, r *http.Request) {
${0:fmt.Fprintf(w, "hello world")}
})
snippet hfr
func ${1:handler}(w http.ResponseWriter, r *http.Request, _ httprouter.Params) {
${0:fmt.Fprintf(w, "hello world")}
}