go.snip 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382
  1. # shorthand variable declaration
  2. snippet :
  3. abbr v := value
  4. ${1} := ${0}
  5. # anonymous function
  6. snippet anon
  7. abbr fn := func() { ... }
  8. ${1:fn} := func() {
  9. ${0}
  10. }
  11. # append
  12. snippet ap
  13. abbr append(slice, value)
  14. append(${1:slice}, ${0:value})
  15. # append assign
  16. snippet ap=
  17. abbr slice = append(slice, value)
  18. ${1:slice} = append($1, ${0:value})
  19. # break
  20. snippet br
  21. abbr break
  22. break
  23. # channel
  24. snippet ch
  25. abbr chan Type
  26. chan ${0:int}
  27. # case
  28. snippet case
  29. abbr case ...:
  30. case ${1:value}:
  31. ${0}
  32. # constant
  33. snippet con
  34. abbr const XXX Type = ...
  35. const ${1:NAME} ${2:Type} = ${0:0}
  36. # constants
  37. snippet cons
  38. abbr const ( ... )
  39. const (
  40. ${1:NAME} ${2:Type} = ${3:value}
  41. ${0}
  42. )
  43. # constants with iota
  44. snippet iota
  45. abbr const ( ... = iota )
  46. const (
  47. ${1:NAME} ${2:Type} = iota
  48. ${0}
  49. )
  50. # continue
  51. snippet cn
  52. abbr continue
  53. continue
  54. # default case
  55. snippet default
  56. abbr default: ...
  57. default:
  58. ${0}
  59. # defer
  60. snippet df
  61. abbr defer someFunction()
  62. defer ${1:func}(${2})
  63. ${0}
  64. snippet def
  65. abbr defer func() { ... }
  66. defer func() {
  67. ${0}
  68. }()
  69. # defer recover
  70. snippet defr
  71. defer func() {
  72. if err := recover(); err != nil {
  73. ${0}
  74. }
  75. }()
  76. # gpl
  77. snippet gpl
  78. /*
  79. * This program is free software; you can redistribute it and/or modify
  80. * it under the terms of the GNU General Public License as published by
  81. * the Free Software Foundation; either version 2 of the License, or
  82. * (at your option) any later version.
  83. *
  84. * This program is distributed in the hope that it will be useful,
  85. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  86. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  87. * GNU General Public License for more details.
  88. *
  89. * You should have received a copy of the GNU General Public License
  90. * along with this program; if not, see <http://www.gnu.org/licenses/>.
  91. *
  92. * Copyright (C) ${1:Author}, `strftime("%Y")`
  93. */
  94. ${0}
  95. # import
  96. snippet import
  97. abbr import ( ... )
  98. import (
  99. "${1:package}"
  100. )
  101. # full interface snippet
  102. snippet interface
  103. abbr interface I { ... }
  104. type ${1:Interface} interface {
  105. ${2:/* TODO: add methods */}
  106. }
  107. # if condition
  108. snippet if
  109. abbr if ... { ... }
  110. if ${1:condition} {
  111. ${0}
  112. }
  113. # else snippet
  114. abbr else { ... }
  115. snippet else
  116. else {
  117. ${0}
  118. }
  119. # if inline error
  120. snippet ife
  121. abbr if err := ...; err != nil { ... }
  122. if err := ${1:condition}; err != nil {
  123. ${0}
  124. }
  125. # error snippet
  126. snippet errn
  127. abbr if err != nil { return err }
  128. if err != nil {
  129. return err
  130. }
  131. ${0}
  132. # error snippet in TestFunc
  133. snippet errt
  134. abbr if err != nil { t.Fatal(err) }
  135. if err != nil {
  136. t.Fatal(err)
  137. }
  138. # error snippet in log.Fatal
  139. snippet errl
  140. abbr if err != nil { log.Fatal(err) }
  141. if err != nil {
  142. log.Fatal(err)
  143. }
  144. # error snippet with two return values
  145. snippet errn,
  146. abbr if err != nil { return [...], err }
  147. if err != nil {
  148. return ${1:nil}, err
  149. }
  150. ${0}
  151. # error snippet handle and return
  152. snippet errh
  153. abbr if err != nil { ... return }
  154. if err != nil {
  155. ${1}
  156. return
  157. }
  158. ${0}
  159. # error snippet with panic
  160. snippet errp
  161. abbr if err != nil { panic(...) }
  162. if err != nil {
  163. panic(${1})
  164. }
  165. ${0}
  166. # json snippet
  167. snippet json
  168. abbr \`json:key\`
  169. \`json:"${1:keyName}"\`
  170. # yaml snippet
  171. snippet yaml
  172. abbr \`yaml:key\`
  173. \`yaml:"${1:keyName}"\`
  174. # fallthrough
  175. snippet ft
  176. abbr fallthrough
  177. fallthrough
  178. # for loop
  179. snippet for
  180. abbr for ... { ... }
  181. for ${1} {
  182. ${0}
  183. }
  184. # for integer loop
  185. snippet fori
  186. abbr for 0..N-1 { ... }
  187. for ${1:i} := 0; $1 < ${2:N}; $1++ {
  188. ${0}
  189. }
  190. # for range loop
  191. snippet forr
  192. abbr for k, v := range items { ... }
  193. for ${2:k}, ${3:v} := range ${1} {
  194. ${0}
  195. }
  196. # function
  197. snippet func
  198. abbr func function(...) [error] { ... }
  199. func ${1:function}(${2}) ${3:error }{
  200. ${0}
  201. }
  202. # Fmt Printf debug
  203. snippet ff
  204. abbr fmt.Printf(...)
  205. fmt.Printf("${1} = %+v\n", $1)
  206. ${0}
  207. # Fmt Println debug
  208. snippet fn
  209. abbr fmt.Println(...)
  210. fmt.Println("${1}")
  211. # Fmt Errorf
  212. snippet fe
  213. abbr fmt.Errorf(...)
  214. fmt.Errorf("${1}")
  215. # log printf
  216. snippet lf
  217. abbr log.Printf(...)
  218. log.Printf("${1} = %+v\n", $1)
  219. # log println
  220. snippet ln
  221. abbr log.Println(...)
  222. log.Println("${1}")
  223. # make
  224. snippet make
  225. abbr make(Type, size)
  226. make(${1:[]string}, ${2:0})${0}
  227. # map
  228. snippet map
  229. abbr map[Type]Type
  230. map[${1:string}]${0:int}
  231. # main()
  232. snippet main
  233. abbr func main() { ... }
  234. options head
  235. func main() {
  236. ${0}
  237. }
  238. # method
  239. snippet meth
  240. abbr func (self Type) Method(...) [error] { ... }
  241. regexp /^meth/
  242. func (${1:self} ${2:Type}) ${3:Do}(${4}) ${5:error }{
  243. ${0}
  244. }
  245. # ok
  246. snippet ok
  247. abbr if !ok { ... }
  248. if !ok {
  249. ${0}
  250. }
  251. # package
  252. snippet package
  253. abbr package ...
  254. // Package $1 provides ${2:...}
  255. package ${1:main}
  256. ${0}
  257. # panic
  258. snippet panic
  259. alias pn
  260. abbr panic("...")
  261. panic("${0}")
  262. # return
  263. snippet return
  264. alias rt
  265. abbr return ...
  266. return ${0}
  267. # select
  268. snippet select
  269. abbr select { case a := <-chan: ... }
  270. select {
  271. case ${1:v1} := <-${2:chan1}
  272. ${0}
  273. }
  274. # struct
  275. snippet st
  276. abbr type T struct { ... }
  277. type ${1:Type} struct {
  278. ${0}
  279. }
  280. # switch
  281. snippet switch
  282. abbr switch x { ... }
  283. switch ${1:var} {
  284. case ${2:value1}:
  285. ${0}
  286. }
  287. # sprintf
  288. snippet sp
  289. abbr fmt.Sprintf(...)
  290. fmt.Sprintf("%${1:s}", ${2:var})
  291. # goroutine named function
  292. snippet go
  293. abbr go someFunc(...)
  294. go ${1:funcName}(${0})
  295. # goroutine anonymous function
  296. snippet gof
  297. abbr go func(...) { ... }(...)
  298. go func(${1}) {
  299. ${3:/* TODO */}
  300. }(${2})
  301. # test function
  302. snippet test
  303. abbr func TestXYZ(t *testing.T) { ... }
  304. func Test${1:Function}(t *testing.T) {
  305. ${0}
  306. }
  307. # test server
  308. snippet tsrv
  309. abbr ts := httptest.NewServer(...)
  310. ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
  311. fmt.Fprintln(w, ${1:`response`})
  312. }))
  313. defer ts.Close()
  314. //Use testing server url (type string) somewhere
  315. ${0:someUrl} = ts.URL
  316. # test error
  317. snippet ter
  318. abbr if err != nil { t.Errorf(...) }
  319. if err != nil {
  320. t.Errorf("${1}")
  321. }
  322. # test fatal error
  323. snippet terf
  324. abbr if err != nil { t.Fatalf(...) }
  325. if err != nil {
  326. t.Fatalf("${1}")
  327. }
  328. # test example
  329. snippet example
  330. func Example${1:Method}() {
  331. ${0}
  332. // Output:
  333. }
  334. # test benchmark
  335. snippet benchmark
  336. func Benchmark${1:Method}(b *testing.B) {
  337. for i := 0; i < b.N; i++ {
  338. ${0}
  339. }
  340. }
  341. # variable declaration
  342. snippet var
  343. abbr var x Type [= ...]
  344. var ${1:x} ${2:Type}${3: = ${0:value\}}
  345. # variables declaration
  346. snippet vars
  347. abbr var ( ... )
  348. var (
  349. ${1:x} ${2:Type}${3: = ${0:value\}}
  350. )
  351. # equals fails the test if exp is not equal to act.
  352. snippet eq
  353. abbr equals: test two identifiers with DeepEqual
  354. if !reflect.DeepEqual(${1:expected}, ${2:actual}) {
  355. _, file, line, _ := runtime.Caller(0)
  356. fmt.Printf("%s:%d:\n\n\texp: %#v\n\n\tgot: %#v\n\n", filepath.Base(file), line, $1, $2)
  357. t.FailNow()
  358. }
  359. snippet hf
  360. abbr http.HandlerFunc
  361. func ${1:handler}(w http.ResponseWriter, r *http.Request) {
  362. ${0:fmt.Fprintf(w, "hello world")}
  363. }
  364. snippet hhf
  365. abbr mux.HandleFunc(...)
  366. ${1:http}.HandleFunc("${2:/}", func(w http.ResponseWriter, r *http.Request) {
  367. ${0:fmt.Fprintf(w, "hello world")}
  368. })