Cimri Go VS Code Snippets
Some of the snippets are from https://github.com/honnamkuan/vscode-golang-snippets/
Installation
Supported languages (file extensions)
Snippets
My Snippets
Prefix |
Content |
getenv |
var envName = config.GetEnv("envName", "envValue") |
icc |
import "bitbucket.org/cimri/cimri-common-go/" |
str |
type structName struct { fieldName string `json:"fieldName"` } |
nstr |
type StructName struct { Field string json:"field" }
func NewStructName(field string) *StructName { return &{ Field: field, } } |
recover |
defer func() { if r := recover(); r != nil {
} }() |
jsm |
j, err := json.Marshal(variable) if err != nil {
} |
jsunm |
var data interface{} json.Unmarshal(byteArr, &data) if err != nil {
} |
enum |
type ENUM_TYPE string const ( ENUM_ONE ENUM_TYPE = "" ) |
logerr |
logger.Error().Msgf("%+v\n", err) |
logfat |
logger.Fatal().Msgf("%+v\n", err) |
loginfo |
logger.Info().Msgf("%+v\n", err) |
logdeb |
logger.Debug().Msgf("%+v\n", err) |
logg |
logger.Log().Msgf("%+v\n", err) |
om |
defer func(begin time.Time) { metric.Observe("MetricName", begin) }(time.Now()) |
omh |
defer func(begin time.Time) { metric.ObserveHandler("MetricName", begin, c.Response().StatusCode()) }(time.Now()) |
now |
var now := time.Now() |
newreq |
req, err := http.NewRequest(http.MethodGet, url, nil) if err != nil {
}
res, err := http.DefaultClient.Do(req) if err != nil {
}
|
get |
res, err := http.Get(url) if err != nil {
}
|
rb |
body, err = io.ReadAll(resp.Body) defer resp.Body.Close() |
pf |
fmt.Printf("%+v\n", ) |
pln |
fmt.Println("") |
spf |
fmt.Sprintf("%+v\n", ) |
errf |
fmt.Errorf("%+v\n", err) |
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 } |
lock |
sync.Mutex Lock and defer Unlock mu.Lock() defer mu.Unlock() |
nb |
non-blocking channel send select { case msg <- msgChan: default: } |
Testify Assert
Prefix |
Content |
anil |
assert nil assert.Nil(t, actual) |
annil |
assert not nil assert.NotNil(t, actual) |
aeq |
assert equal assert.Equal(t, expected, actual) |
anerr |
assert no error assert.NoError(t, err) |
| |