# Snippets for Go

# when to abbriviate and when not?
# b doesn't work here, because it ignores whitespace
# optional local name?
snippet import "Import declaration" b
	import (
		"${1:package}"
	)

snippet package "Package declaration" b
	// Package $1 provides ...
	package ${1:main}

# Mostly converted from: https://github.com/AlanQuatermain/go-tmbundle
snippet cons "Constants declaration" b
	const (
		${1:constant}${2/(.+)/ /}${2:type} = ${0:value}
	)

snippet con "Constant declaration" b
	const ${1:name}${2/(.+)/ /}${2:type} = ${0:value}

snippet iota "Iota constant generator" b
	const (
		${1:constant}${2/(.+)/ /}${2:type} = iota
	)

snippet struct "Struct declaration"
	type ${1:Struct} struct {
		${0:${VISUAL}}
	}

snippet interface "Interface declaration" b
	type ${1:Interface} interface {
		${0:${VISUAL}}
	}

snippet if "If statement" b
	if ${1:condition}${1/(.+)/ /}{
		${0:${VISUAL}}
	}

snippet iferr "Error checking if statement" b
	if ${1:err} != nil {
		return ${2:val}${2/.+/, /}fmt.Errorf("${0:${VISUAL}}: %w", ${1})
	}

snippet for "For statement" b
	for ${1:test}${1/(.+)/ /}{
		${0:${VISUAL}}
	}

snippet range "For i := range statement" b
	for ${1:i} := range ${2:list} {
		${0:${VISUAL}}
	}

snippet rangeitem "For i, item := range statement" b
	for ${1:i}, ${2:item} := range ${3:list} {
		${0:${VISUAL}}
	}

snippet switch "Switch statement" b
	switch ${1:expression}${1/(.+)/ /}{
	case ${2:value}:
		$0
	}

snippet case "Case statement" b
	case ${1:value}:
		$0

# functions
snippet main "Main function" b
	func main() {
		${0:${VISUAL}}
	}

snippet meth "Method" b
	func (${1:receiver} ${2:type}) ${3:name}(${4:params})${5/(.+)/ /}${5:type} {
		${0:${VISUAL}}
	}

snippet func "Function" b
	func ${1:name}(${2:params})${3/(.+)/ /}${3:type} {
		${0:${VISUAL}}
	}

snippet funch "HTTP handler" b
	func ${1:handler}(${2:w} http.ResponseWriter, ${3:r} *http.Request) {
		${0:${VISUAL}}
	}

# types and variables
snippet map "Map type"
	map[${1:keytype}]${2:valtype}

snippet : "Variable declaration :=" b
	${1:name} := ${0:value}

snippet var "Variable declaration" b
	var ${1:name}${2/(.+)/ /}${2:type}${3: = ${0:value}}

snippet vars "Variables declaration" b
	var (
		${1:name}${2/(.+)/ /}${2:type}${3: = ${0:value} }
	)

snippet json "JSON field"
	\`json:"${1:displayName}"\`

snippet convey "Convey test"
	Convey("${1:desc}", func() {
		${0}
	})

# vim:ft=snippets:
