go.snippets 7.8 KB

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