List of Go postfix completion templates
This table summarizes the postfix completion templates that you can use with your Go code.
Name | Description | Example. Before | Example. After |
---|---|---|---|
| Negates the boolean expression. |
func foo(b bool) {
if b! {}
}
|
func foo(b bool) {
if !b {}
}
|
| Adds the & operator before the expression. |
func foo() *string {
s := "ok"
return s&
}
|
func foo() *string {
s := "ok"
return &s
}
|
| Creates a dereference expression. |
func m() {
ptr := new(int)
i := ptr*
}
|
func m() {
ptr := new(int)
i := *ptr
}
|
| Wraps the expression with the append() built-in function and assigns its result to the expression. |
func m() {
slice.aappend
}
|
func m() {
slice = append(slice, )
}
|
| Wraps the expression with the append() built-in function. |
func m() {
slice.append
}
|
func m() {
append(slice, )
}
|
| Wraps the expression with the append() built-in function and assigns its result to the expression. |
func m() {
slice.appendAssign
}
|
func m() {
slice = append(slice, )
}
|
| Wraps the expression with the errors.As() function. |
func f() {
var err error
err.as
}
|
func f() {
var err error
errors.As(err)
}
|
| Wraps the expression with the cap() built-in function. |
func m() {
array.cap
}
|
func m() {
cap(array)
}
|
| Wraps the expression with the close() built-in function. |
func m() {
channel.close
}
|
func m() {
close(channel)
}
|
| Wraps the expression with the complex() built-in function. |
func m() {
number.complex
}
|
func m() {
complex(number, )
}
|
| Wraps the expression with the copy() built-in function. |
func m() {
array.copy
}
|
func m() {
copy(array, )
}
|
| Creates a dereference expression. |
func m() {
ptr := new(int)
i := ptr.d
}
|
func m() {
ptr := new(int)
i := *ptr
}
|
| Wraps the expression with the delete() built-in function. |
func m() {
mapVariable.delete
}
|
func m() {
delete(mapVariable, )
}
|
| Creates a dereference expression. |
func m() {
ptr := new(int)
i := ptr.dereference
}
|
func m() {
ptr := new(int)
i := *ptr
}
|
| Turns the E expression into if !E {}. |
func m(b bool) {
b.else
}
|
func m(b bool) {
if !b {
}
}
|
| Creates the range form of the for loop to iterate over a slice or an array. |
func m(arr []byte) {
arr.for
}
|
func m(arr []byte) {
for e := range arr {
}
}
|
| Creates the range form of the for loop with an index and a value to iterate over a slice or an array. |
func m(arr []byte) {
arr.forr
}
|
func m(arr []byte) {
for i, b := range arr {
}
}
|
| Turns the E expression into if E {}. |
func m(b bool) {
b.if
}
|
func m(b bool) {
if b {
}
}
|
| Wraps the expression with the imag() built-in function. |
func m() {
number.imag
}
|
func m() {
imag(number)
}
|
| Wraps the expression with the errors.Is() function. |
func f() {
var err error
err.is
}
|
func f() {
var err error
errors.Is(err)
}
|
| Wraps the expression with the len() built-in function. |
func m() {
array.len
}
|
func m() {
len(array)
}
|
| Wraps the expression with the if statement that checks whether the expression is nil. |
func m(arg interface{}) {
arg.nil
}
|
func m(arg interface{}) {
if arg == nil {
}
}
|
| Checks whether the if condition expression is nil. |
func _() {
if x := foo(); x.nil
}
|
func _() {
if x := foo(); x == nil {
}
}
|
| Wraps the expression with the if statement that checks whether the expression is not nil. |
func m(arg interface{}) {
arg.nn
}
|
func m(arg interface{}) {
if arg != nil {
}
}
|
| Checks whether the if condition expression is not nil. |
func _() {
if err := foo(); err.nn
}
|
func _() {
if err := foo(); err != nil {
}
}
|
| Negates the boolean expression. |
func foo(b bool) {
if b.not {}
}
|
func foo(b bool) {
if !b {}
}
|
| Wraps the expression with the if statement that checks whether the expression is not nil. |
func m(arg interface{}) {
arg.notnil
}
|
func m(arg interface{}) {
if arg != nil {
}
}
|
| Checks whether the if condition expression is not nil. |
func _() {
if err := foo(); err.notnil
}
|
func _() {
if err := foo(); err != nil {
}
}
|
| Adds the & operator before the expression. |
func foo() *string {
s := "ok"
return s.p
}
|
func foo() *string {
s := "ok"
return &s
}
|
| Wraps the expression with the panic() built-in function. |
func m() {
"expression".panic
}
|
func m() {
panic("expression")
}
|
| Wraps the expression with parentheses. |
func m(arg interface{}) {
arg.par
}
|
func m(arg interface{}) {
(arg)
}
|
| Generates the code to parse float64 from string. |
func foo() {
"12.34".parseFloat
}
|
func foo() {
value, err := strconv.ParseFloat("123", 64)
}
|
| Generates the code to parse int from string. |
func foo() {
"123".parseInt
}
|
func foo() {
value, err := strconv.ParseInt("123", 10, 64)
}
|
| Adds the & operator before the expression. |
func foo() *string {
s := "ok"
return s.pointer
}
|
func foo() *string {
s := "ok"
return &s
}
|
| Wraps the expression with the print() built-in function. |
func m() {
"expression".print
}
|
func m() {
print("expression")
}
|
| Wraps the expression with the println() built-in function. |
func m() {
"expression".println
}
|
func m() {
println("expression")
}
|
| Wraps the expression with the real() built-in function. |
func m() {
number.real
}
|
func m() {
real(number)
}
|
| Creates a copy of a slice with a range of elements removed. For more information about operations with slices, see Slice Tricks in the golang repository at github.com. |
func m() {
slice.remove
}
|
func m() {
slice = append(slice[:1], slice[2:]...)
}
|
| Generates boilerplate code for idiomatic error handling. Can be called on variables and expressions of the error type. Replaces the err variable with the following construction: if err != nil { return value1, value2, ... } Replaces the expr expression with the following construction: if err := expr; err != nil { return value1, value2, ... } Each value is either the err variable or a default value of the corresponding return type of the enclosing function. You can call this template on expressions with multiple values. |
func read(file string) (int, error) {
f, err := os.Open(file)
err.reterr
}
func read(file *os.File, data []byte) (int, error) {
file.Write(data).reterr
}
|
func read(file string) (int, error) {
f, err := os.Open(file)
if err != nil {
return 0, err
}
}
func read(file *os.File, data []byte) (int, error) {
if _, err := file.Write(data); err != nil {
return 0, err
}
}
|
| Adds the return keyword before the expression. |
func m() {
"result".return
}
|
func m() {
return "result"
}
|
| Generates boilerplate code for idiomatic error handling. Can be called on variables and expressions of the error type. Replaces variable err with the following construction: if err != nil { return value1, value2, ... } Replaces expression expr with the following construction: if err := expr; err != nil { return value1, value2, ... } Each value is either the err variable or a default value of the corresponding return type of the enclosing function. You can call this template on expressions with multiple values. |
func read(file string) (int, error) {
f, err := os.Open(file)
err.rr
}
func read(file *os.File, data []byte) (int, error) {
file.Write(data).rr
}
|
func read(file string) (int, error) {
f, err := os.Open(file)
if err != nil {
return 0, err
}
}
func read(file *os.File, data []byte) (int, error) {
if _, err := file.Write(data); err != nil {
return 0, err
}
}
|
| Wraps the expression with the sort.Float64s() function. |
func foo() {
data := []float64{}
data.sort
}
|
func foo() {
data := []float64{}
sort.Float64s(data)
}
|
| Wraps the expression with the sort.Ints() function. |
func foo() {
data := []int{}
data.sort
}
|
func foo() {
data := []int{}
sort.Ints(data)
}
|
| Wraps the expression with the sort.Slice() function. |
func foo() {
data := []struct{a string}{}
data.sort
}
|
func foo() {
data := []struct{a string}{}
sort.Slice(data, func(i, j int) bool {
return false
})
}
|
| Wraps the expression with the sort.Sort() function. |
package foo
func _() {
people := []Person{}
ByAge(people).sort
}
type Person struct {
Name string
Age int
}
// ByAge implements sort.Interface for []Person based on
// the Age field.
type ByAge []Person
func (a ByAge) Len() int { return len(a) }
func (a ByAge) Swap(i, j int) { a[i], a[j] = a[j], a[i] }
func (a ByAge) Less(i, j int) bool { return a[i].Age < a[j].Age }
|
package foo
func _() {
people := []Person{}
sort.Sort(ByAge(people))
}
type Person struct {
Name string
Age int
}
// ByAge implements sort.Interface for []Person based on
// the Age field.
type ByAge []Person
func (a ByAge) Len() int { return len(a) }
func (a ByAge) Swap(i, j int) { a[i], a[j] = a[j], a[i] }
func (a ByAge) Less(i, j int) bool { return a[i].Age < a[j].Age }
|
| Wraps the expression with the sort.Strings() function. |
func foo() {
data := []string{}
data.sort
}
|
func foo() {
data := []string{}
sort.Strings(data)
}
|
| Assigns the expression to a new variable by using :=. |
func m(arg interface{}) {
arg.var
}
|
func m(arg interface{}) {
i := arg
}
|
| Generates boilerplate code for idiomatic error handling. You can invoke the template on calls that return error or on any expressions of the error type. The template introduces new variables, assigns the expression to them, and checks if error is not nil by using the following code: if err != nil { return value1, value2, ... } Each value is either the err variable or a default value of the corresponding return type of the enclosing function. |
func read(file string) (int, error) {
os.Open(file).varCheckError
}
|
func read(file string) (int, error) {
f, err := os.Open(file)
if err != nil {
return 0, err
}
}
|