If you find this extension useful, please consider to buy me a coffee ☕ 😉
This extension contains code snippets for Go language in VS Code.
Installation
In order to install an extension you need to launch the Command Palette (Ctrl + Shift + P or Cmd + Shift + P) and type Extensions.
There you have either the option to show the already installed snippets or install new ones. Search for honnamkuan.golang-snippets and install it.
Supported languages (file extensions)
Go (.go)
Snippets
Below is a list of all available snippets and the triggers of each one.
General
Prefix
Content
vv
initialize variable varName := value
ier
if error if err != nil { myStatements }
ifok
if ok if value,ok := myFunc; ok { myStatements }
fr
for range for _, v := range values { myStatements }
frr
for range channel for v := range channel { myStatements }
def
case default default:
cl
close close(closable)
fms
fmt Sprintf fmt.Sprintf("%+v", args)
fme
fmt Errorf fmt.Errorf("%+v", args)
ctx
ctx context.Context
Types
Prefix
Content
st
struct type
type structName struct { }
sf
struct field fieldName string
stt
struct tag `json:"jsonFieldName"`
ne
struct constructor method
func NewFoo() *Foo{ return &Foo { } }
inte
Interface type
type interfaceName interface { }
Collection manipulation
Prefix
Content
sr
remove one element from slice slice = append(slice[:index], slice[index+1:]...)
ap
append element to slice slice = append(slice, element)
del
delete map element by key delete(map, key)
Return values
Prefix
Content
rn
Return Nil return nil
rne
Return Nil and err return nil, err
re
Return err return err
Logging
Prefix
Content
lo
log variable log.Printf("%+v\n", varName)
le
log error log.Printf("%+v\n", err)
lef
log error (when using logrus) log.Errorf("%+v\n", err)
lf
log fatal log.Fatal(err)
lff
log fatal log.Fatalf("%+v\n", err)
Error Handling
Prefix
Content
es
errors with stack errors.WithStack(err)
em
error with message errors.WithMessage(err, message)
emf
error with messagef errors.WithMessagef(err, format, args)
is
errors Is if errors.Is(err, MyError) { myStatements }
as
errors As
var e ErrorType errors.As(err, &e) { myStatements }
Concurrency
Prefix
Content
gofunc
anonymous go function go func() { myStatements }
defunc
anonymous defer function defer func { myStatements }