source go/go.vim # From neosnippet-snippets {{{ # https://github.com/Shougo/neosnippet-snippets/blob/master/neosnippets/go.snip snippet func abbr func ...() { ... } alias fun options head func ${1:fname}(${2}) ${3:int }{ ${0:TARGET:return } } snippet struct alias ts options head type ${1} struct { ${0:TARGET} } snippet interface alias ti options head type ${1} interface { ${0:TARGET} } snippet for options head for ${1} { ${0:TARGET} } snippet forr abbr for range options head for ${1:_}, ${2:v} := range ${3:#:iterator} { ${0:TARGET} } snippet if options head if ${1:#:condition} { ${0:TARGET} } snippet ifel options head if ${1:#:condition} { ${2} } else { ${3} } snippet iferr alias ife options head if err != nil { return `g:Neosnippet_go_iferr()` } snippet switch abbr switch {} alias sw options head switch ${1:#:v} { case ${2:#:condition}: ${0:TARGET} } snippet select abbr select {} alias sel options head select { case ${1:#:condition}: ${0:TARGET} } snippet test abbr func Test... (t *testing.T) { ... } alias test options head func Test${1} (${2:t *testing.T}) { for i := 0; i < ${3:t.N}; i++ { ${4} } } snippet bench abbr func Benchmark... (b *testing.B) { ... } alias bench options head func Benchmark${1} (${2:b *testing.B}) { for i := 0; i < ${3:b.N}; i++ { ${4} } } snippet fuzz abbr func Fuzz...(f *testing.F) { ... } options head func Fuzz${1}(${2:f *testing.F}) { f.Fuzz(func(t *testing.T, ${3:b []byte}) { ${4} }) } # test table snippet tt abbr var test = {...}{...} for {t.Run(){...}} options head var tests = []struct { name string expected string given string }{ {"${2}", "${3}", "${4}",}, } for _, tt := range tests { t.Run(tt.name, func(t *testing.T){ actual := ${1:Function}(tt.given) if actual != tt.expected { t.Errorf("given(%s): expected %s, actual %s", tt.given, tt.expected, actual) } }) } # }}} # From vim-go {{{ # https://github.com/fatih/vim-go/blob/master/gosnippets/snippets/go.snip snippet append abbr slice = append(slice, newItems...) alias ap options head ${1:slice} = append($1, ${0:newItems...}) snippet defer abbr defer func() { ... } alias def options head defer func() { ${0} }() snippet defrecover abbr defer func() { ... } alias defr options head defer func() { if err := recover(); err != nil { ${0} } }() snippet fori abbr for 0..N-1 { ... } options head for ${1:i} := 0; $1 < ${2:N}; $1++ { ${0} } snippet ff abbr fmt.Printf(...) options head fmt.Printf("${1} = %+v\n", $1)${0} snippet fln abbr fmt.Println(...) options head fmt.Println("${1}")${0} snippet method abbr func (self Type) Method(...) [error] { ... } alias meth options head func (`g:Neosnippet_go_method_receiver()`) ${3:Do}(${4}) ${5:error }{ ${0} } snippet gof abbr go func(...) { ... }(...) options head go func(${1}) { ${3:/* TODO */} }(${2}) # }}} snippet ctx abbr ctx context.Context ctx context.Context snippet cb abbr context.Background() context.Background() snippet cc abbr context with cancel options head ctx, ${1:cancel} := context.WithCancel(${2:context.Background()}) defer $1() snippet ct abbr context with timeout options head ctx, ${1:cancel} := context.WithTimeout(${2:context.Background()}, ${3:timeout}) ${4:defer $1()} snippet json abbr \`json:key\` \`json:"${1:`g:Neosnippet_go_json_tagname()`}"\`${0} snippet itoa abbr strconv.Itoa strconv.Itoa(${1})${0} snippet atoi abbr strconv.Atoi strconv.Atoi(${1})${0} snippet req abbr http request options head req, err := http.NewRequest(${1:http.MethodGet}, ${2:url}, ${3:nil}) if err != nil { return err } req = req.WithContext(ctx) req.Header.Set(${4:key}, ${5:value}) q := req.URL.Query() q.Add("${6:key}", "${7:val}") req.URL.RawQuery = q.Encode() resp, err := ${8:httpClient}.Do(req) if err != nil { return err } defer resp.Body.Close() body, err := ioutil.ReadAll(resp.Body) if err != nil { return err } if resp.StatusCode != http.StatusOK { return errors.New("got status " + resp.Status + " with response body: " + string(body)) } ${0} snippet lock abbr lock with RWMutex options head ${1:lock}.Lock() defer $1.Unlock() ${0} snippet rlock abbr rlock with RWMutex options head ${1:lock}.RLock() defer $1.RUnlock() ${0} snippet getEnvOrPanic abbr getEnvOrPanic options head func getEnvOrPanic(key string) string { val, ok := os.LookupEnv(key) if !ok { panic(fmt.Sprintf("env var %s is not defined", key)) } return val } ${0} snippet sort options head sort.Slice(${1:slice}, func(i, j int) bool { return $1[i].${2:field} < $1[j].$2 }) ${0} snippet sortr abbr sort reverse options head sort.Sort(sort.Reverse(sort.IntSlice(${1:slice}))) snippet sorti options head sort.Ints(${1:intSlice}) ${0} snippet dd abbr dbg(...) options head dbg("${1} = %+v\n", $1)${0} # vim: foldmethod=marker