vimrc/sources_non_forked/vim-snippets/snippets/go.snippets

281 lines
4 KiB
Text
Raw Normal View History

2018-09-30 20:58:57 +00:00
snippet v "shorthand variable declaration"
${1} := ${2}
2018-09-30 20:58:57 +00:00
snippet vr "variable initialization"
var ${1:t} ${0:string}
2018-09-30 20:58:57 +00:00
snippet var "variable declaration"
var ${1} ${2} = ${3}
2018-09-30 20:58:57 +00:00
snippet vars "variables declaration"
var (
${1} ${2} = ${3}
)
2018-09-30 20:58:57 +00:00
snippet ap "append"
append(${1:slice}, ${0:value})
2018-09-30 20:58:57 +00:00
snippet bl "bool"
2012-08-17 03:41:25 +00:00
bool
2018-09-30 20:58:57 +00:00
snippet bt "byte"
2012-08-17 03:41:25 +00:00
byte
2018-09-30 20:58:57 +00:00
snippet br "break"
2012-08-17 03:41:25 +00:00
break
2018-09-30 20:58:57 +00:00
snippet ch "channel"
chan ${0:int}
2018-09-30 20:58:57 +00:00
snippet cs "case"
2012-08-17 03:41:25 +00:00
case ${1:value}:
2017-02-11 13:01:38 +00:00
${0:${VISUAL}}
2018-09-30 20:58:57 +00:00
snippet co "constants with iota"
2012-08-17 03:41:25 +00:00
const (
${1:NAME1} = iota
${0:NAME2}
2012-08-17 03:41:25 +00:00
)
2018-09-30 20:58:57 +00:00
snippet cn "continue"
2012-08-17 03:41:25 +00:00
continue
2018-09-30 20:58:57 +00:00
snippet df "defer"
defer ${0:func}()
2018-09-30 20:58:57 +00:00
snippet dfr "defer recover"
2012-08-17 03:41:25 +00:00
defer func() {
if err := recover(); err != nil {
2017-02-11 13:01:38 +00:00
${0:${VISUAL}}
2012-08-17 03:41:25 +00:00
}
}()
2018-09-30 20:58:57 +00:00
snippet im "import"
2012-08-17 03:41:25 +00:00
import (
2013-07-17 23:06:05 +00:00
"${1:package}"
)
2018-09-30 20:58:57 +00:00
snippet in "interface"
2012-08-17 03:41:25 +00:00
interface{}
2018-09-30 20:58:57 +00:00
snippet inf "full interface "
2012-08-17 03:41:25 +00:00
interface ${1:name} {
${2:/* methods */}
}
2018-09-30 20:58:57 +00:00
snippet if "if condition"
if $1 {
2017-02-11 13:01:38 +00:00
${2:${VISUAL}}
}
2018-09-30 20:58:57 +00:00
snippet ife "if else condition"
if $1 {
2017-02-11 13:01:38 +00:00
${2:${VISUAL}}
2016-03-14 10:04:57 +00:00
} else {
${0}
}
2018-09-30 20:58:57 +00:00
snippet el "else"
2012-08-17 03:41:25 +00:00
else {
2017-02-11 13:01:38 +00:00
${0:${VISUAL}}
}
2018-09-30 20:58:57 +00:00
snippet ir "if error not nil, return err"
2012-08-17 03:41:25 +00:00
if err != nil {
return err
}
${0}
2018-09-30 20:58:57 +00:00
snippet f "false"
2012-08-17 03:41:25 +00:00
false
2018-09-30 20:58:57 +00:00
snippet ft "fallthrough"
2012-08-17 03:41:25 +00:00
fallthrough
2018-09-30 20:58:57 +00:00
snippet fl "float"
2012-08-17 03:41:25 +00:00
float32
2018-09-30 20:58:57 +00:00
snippet f3 "float32"
2012-08-17 03:41:25 +00:00
float32
2018-09-30 20:58:57 +00:00
snippet f6 "float64"
2012-08-17 03:41:25 +00:00
float64
2018-09-30 20:58:57 +00:00
snippet for "for loop"
2016-03-14 10:04:57 +00:00
for ${1}{
2017-02-11 13:01:38 +00:00
${0:${VISUAL}}
2016-03-14 10:04:57 +00:00
}
2018-09-30 20:58:57 +00:00
snippet fori "for int loop"
for ${2:i} := 0; $2 < ${1:count}; $2${3:++} {
2017-02-11 13:01:38 +00:00
${0:${VISUAL}}
2012-08-17 03:41:25 +00:00
}
2018-09-30 20:58:57 +00:00
snippet forr "for range loop"
2016-03-14 10:04:57 +00:00
for ${1:e} := range ${2:collection} {
2017-02-11 13:01:38 +00:00
${0:${VISUAL}}
2012-08-17 03:41:25 +00:00
}
2018-09-30 20:58:57 +00:00
snippet fun "function"
func ${1:funcName}(${2}) ${3:error} {
2013-07-17 23:06:05 +00:00
${4}
2012-08-17 03:41:25 +00:00
}
${0}
2018-09-30 20:58:57 +00:00
snippet fum "method"
2015-01-18 12:58:28 +00:00
func (${1:receiver} ${2:type}) ${3:funcName}(${4}) ${5:error} {
${6}
2012-08-17 03:41:25 +00:00
}
${0}
2018-09-30 20:58:57 +00:00
2020-12-04 21:15:32 +00:00
snippet fumh "http handler function on receiver"
2018-06-14 10:31:12 +00:00
func (${1:receiver} ${2:type}) ${3:funcName}(${4:w} http.ResponseWriter, ${5:r} *http.Request) {
${0:${VISUAL}}
}
2018-09-30 20:58:57 +00:00
snippet lf "log printf"
log.Printf("%${1:s}", ${2:var})
2018-09-30 20:58:57 +00:00
snippet lp "log println"
log.Println("${1}")
2018-09-30 20:58:57 +00:00
snippet mk "make"
make(${1:[]string}, ${0:0})
2018-09-30 20:58:57 +00:00
snippet mp "map"
map[${1:string}]${0:int}
2018-09-30 20:58:57 +00:00
snippet main "func main()"
2012-08-17 03:41:25 +00:00
func main() {
2013-07-17 23:06:05 +00:00
${1}
2012-08-17 03:41:25 +00:00
}
${0}
2018-09-30 20:58:57 +00:00
snippet nw "new"
new(${0:type})
2018-09-30 20:58:57 +00:00
snippet pa "package"
2015-01-18 12:58:28 +00:00
package ${1:main}
2018-09-30 20:58:57 +00:00
snippet pn "panic"
panic("${0:msg}")
2018-09-30 20:58:57 +00:00
snippet pf "fmt.Printf()"
fmt.Printf("%${1:s}\n", ${2:var})
2018-09-30 20:58:57 +00:00
snippet pl "fmt.Println()"
2016-08-02 12:48:32 +00:00
fmt.Println("${1:s}")
2018-09-30 20:58:57 +00:00
snippet rn "range"
range ${0}
2018-09-30 20:58:57 +00:00
snippet rt "return"
return ${0}
2018-09-30 20:58:57 +00:00
snippet rs "result"
2012-08-17 03:41:25 +00:00
result
2018-09-30 20:58:57 +00:00
snippet sl "select"
2012-08-17 03:41:25 +00:00
select {
case ${1:v1} := <-${2:chan1}
2013-07-17 23:06:05 +00:00
${3}
2012-08-17 03:41:25 +00:00
default:
${0}
2012-08-17 03:41:25 +00:00
}
2018-09-30 20:58:57 +00:00
snippet sr "string"
2012-08-17 03:41:25 +00:00
string
2018-09-30 20:58:57 +00:00
snippet st "struct"
2012-08-17 03:41:25 +00:00
struct ${1:name} {
${2:/* data */}
}
${0}
2018-09-30 20:58:57 +00:00
snippet sw "switch"
2012-08-17 03:41:25 +00:00
switch ${1:var} {
case ${2:value1}:
2013-07-17 23:06:05 +00:00
${3}
2012-08-17 03:41:25 +00:00
case ${4:value2}:
2013-07-17 23:06:05 +00:00
${5}
2012-08-17 03:41:25 +00:00
default:
${0}
2012-08-17 03:41:25 +00:00
}
2018-09-30 20:58:57 +00:00
snippet ps "fmt.Sprintf"
fmt.Sprintf("%${1:s}", ${2:var})
2018-09-30 20:58:57 +00:00
snippet t "true"
2012-08-17 03:41:25 +00:00
true
2018-09-30 20:58:57 +00:00
snippet g "goroutine named function"
go ${1:funcName}(${0})
2018-09-30 20:58:57 +00:00
snippet ga "goroutine anonymous function"
2013-08-03 12:50:12 +00:00
go func(${1} ${2:type}) {
${3:/* code */}
}(${0})
2018-09-30 20:58:57 +00:00
snippet test "test function"
2014-07-02 11:18:18 +00:00
func Test${1:name}(t *testing.T) {
2017-02-11 13:01:38 +00:00
${0:${VISUAL}}
2014-07-02 11:18:18 +00:00
}
2018-09-30 20:58:57 +00:00
2019-11-16 15:28:42 +00:00
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}}
})
}
}
2018-09-30 20:58:57 +00:00
snippet bench "benchmark function"
2014-07-02 11:18:18 +00:00
func Benchmark${1:name}(b *testing.B) {
for i := 0; i < b.N; i++ {
${2}
}
}
${0}
2018-09-30 20:58:57 +00:00
snippet cl "composite literals"
2017-07-06 12:57:35 +00:00
type ${1:name} struct {
${2:attrName} ${3:attrType}
}
2018-09-30 20:58:57 +00:00
snippet om "if key in a map"
2017-07-06 12:57:35 +00:00
if ${1:value}, ok := ${2:map}[${3:key}]; ok == true {
${4:/* code */}
}
2018-09-30 20:58:57 +00:00
snippet gg "Grouped globals with anonymous struct"
2017-07-06 12:57:35 +00:00
var ${1:var} = struct{
${2:name} ${3:type}
}{
$2: ${4:value},
}
2018-09-30 20:58:57 +00:00
snippet ja "Marshalable json alias"
2017-07-06 12:57:35 +00:00
type ${1:parentType}Alias $1
func (p *$1) MarshalJSON() ([]byte, error) {
return json.Marshal(&struct{ *$1Alias }{(*$1Alias)(p)})
}
2018-09-25 00:40:17 +00:00
2018-09-30 20:58:57 +00:00
2021-10-11 09:30:43 +00:00
snippet errwr "Error handling with fmt.Errorf"
2018-09-25 00:40:17 +00:00
if ${1}err != nil {
2021-10-11 09:30:43 +00:00
return fmt.Errorf("${2} %w", err)
2018-09-25 00:40:17 +00:00
}