Giter VIP home page Giter VIP logo

goland-live-templates's Introduction

Golang code templates

Useful live templates and code completion for Goland IDE.

Install settings.zip: https://www.jetbrains.com/help/go/sharing-live-templates.html#import

Golang

if err not nil return

Checks that err is not nil and writes return statement with default return values, except last - error. It would be wrapped with errors.Wrap. errors.Wrap can be easily replaced with fmt.Errorf or any other function

Abbreviation:

errr

Template text:

if $ERR$ != nil {
 return $RETURN$errors.Wrap($ERR$, "$TEXT$")$END$
}

Applicable in Go: statement

Name Expression Default value Skip if defined
$ERR$ errorVariable() "err"
$RETURN$ regularExpression(defaultReturnValues, "([^,]*$)", "")
$TEXT$ errorVariable()

errors.Wrap

Abbreviation:

wrap

Template text:

$END$ errors.Wrap($ERR$, "$TEXT$")

Applicable in Go: expression, statement

Name Expression Default value Skip if defined
$ERR$ errorVariable() "err"
$TEXT$ errorVariable()

Variadic slice declaration

Creates shortcut for in-place slice declarations. Compiler should inline function call, so this function free to use. Was created as a replacement for constructions like []string{} -> ss(), []int{} -> is(), etc.

Abbreviation:

ss

Template text:

func $NAME$$END$(slice ...$TYPE$) []$TYPE$ { return slice }

Applicable in Go: file

Name Expression Default value Skip if defined
$TYPE$ "string"
$NAME$ "ss"

Json field names in camelCase

Sometimes we need to declare json fields not in snake_case format (goland used it by default), but in camelCase. This pattern also can be applied to any other tags, like sql, pg, csv.

Abbreviation:

jsonc

Template text:

json:"$FIELD_NAME$"

Applicable in Go: tag literal

Name Expression Default value Skip if defined
$FIELD_NAME$ camelCase(fieldName())

Sort

Generate sort interface for your type. When go core team would add generics you may delete this.

Abbreviation:

sort

Template text:

type $SLICE$ []$TYPE$
func ($REC$ $SLICE$) Len() int           { return len($REC$) }
func ($REC$ $SLICE$) Swap(i, j int)      { $REC$[i], $REC$[j] = $REC$[j], $REC$[i] }
func ($REC$ $SLICE$) Less(i, j int) bool {
    panic("implement me")$END$
}

Applicable in Go: file

Name Expression Default value Skip if defined
$TYPE$ complete() "Some"
$SLICE$ "SomeSlice"
$REC$ "ss"

In

Checks that value is in slice of some type. In easy way.

Abbreviation:

in

Template text:

func isIn$Type$Slice(v $type$, values ...$type$) bool {
	for i := range values {
		if values[i] == v {
			return true
		}
	}
	return false
}

Applicable in Go: file

Name Expression Default value Skip if defined
$type$ "string"
$Type$ "String"

Safe getter

Generates getter for struct field in protoc-gen-go style. Why do you need it? It allows access to nested fields without worrying about nil pointers.

type Value struct {
    Field1 *Struct1
}
type Struct1 struct {
    Field2 *Struct2
}
type Struct2 struct {
    UsefulString *string
}

var value *Value
var _ = value.SafeField1().SafeField2().SafeUsefulString()

func (v *Value) SafeField1() *Struct1 {
	if v == nil {
		return nil
	}
	return v.Field1
}
func (s *Struct1) SafeField2() *Struct2 {
	if s == nil {
		return nil
	}
	return s.Field2
}
func (s *Struct2) SafeUsefulString() *string {
	if s == nil {
		return nil
	}
	return s.UsefulString
}

Abbreviation:

getter

Template text:

func ($RECEIVER$ *$TYPE_1$) Safe$NAME$() $TYPE_2$ {
	if $RECEIVER$ == nil {
		return $RESULT$$END$
	}
	return $RECEIVER$.$NAME$
}

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.