Skip to content

Instantly share code, notes, and snippets.

@xeoncross
Last active December 17, 2019 19:21
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save xeoncross/e26cd59e57cee5db5c2c389d6a002bee to your computer and use it in GitHub Desktop.
Save xeoncross/e26cd59e57cee5db5c2c389d6a002bee to your computer and use it in GitHub Desktop.
Atom IDE snippets for Go
# Your snippets
#
# Atom snippets allow you to enter a simple prefix in the editor and hit tab to
# expand the prefix into a larger code block with templated values.
#
# You can create a new snippet in this file by typing "snip" and then hitting
# tab.
#
# An example CoffeeScript snippet to expand log to console.log:
#
# '.source.coffee':
# 'Console log':
# 'prefix': 'log'
# 'body': 'console.log $1'
#
# Each scope (e.g. '.source.coffee' above) can only be declared once.
#
# This file uses CoffeeScript Object Notation (CSON).
# If you are unfamiliar with CSON, you can read more about it in the
# Atom Flight Manual:
# http://flight-manual.atom.io/using-atom/sections/basic-customization/#_cson
'.source.go':
'Table Driven Test':
'prefix': 'tdt'
'body': """
func Test$1(t *testing.T) {
testCases := []struct {
desc string
$2
}{
{
desc: "$3",
$4
},
}
for _, tC := range testCases {
t.Run(tC.desc, func(t *testing.T) {
$0
})
}
}
"""
'Context WithTimeout':
'prefix': 'ct'
'body': """
ctx, cancel := context.WithTimeout($0, ${1:time.Second})
defer cancel()
"""
'Reverse Slice':
'prefix': 'reverse'
'body': """
for i, j := 0, len($1)-1; i < j; i, j = i+1, j-1 {
$1[i], $1[j] = $1[j], $1[i]
}
"""
'Go inline':
'prefix': 'go'
'body': 'go func() { $0 }()'
'Inspect JSON':
'prefix': 'ij'
'body': """
{
enc := json.NewEncoder(os.Stderr)
enc.SetIndent("", " ")
enc.Encode($0)
}
"""
'For range':
'prefix': 'fr'
'body': """
for _, $1 := range $2 {
$0
}
"""
'Benchmark':
'prefix': 'tb'
'body': """
func Benchmark$1(b *testing.B) {
for i := 0; i < b.N; i++ {
$0
}
}
"""
'HTTP Handler Function':
'prefix': 'ha'
'body': """
func (w http.ResponseWriter, r *http.Request){$0}
"""
'Test function':
'prefix': 't'
'body': """
func Test$1(t *testing.T) {
$0
}
"""
'Test file':
'prefix': 'tf'
'body': """
package $1
import (
"testing"
)
func Test$2(t *testing.T) {
$0
}
"""
'If Type Assertion':
'prefix': 'ita'
'body': """
if $1, ok := $2.($3); ok {
$0
}
"""
'If Error':
'prefix': 'ie'
'body': """
if err != nil {
$0
}
"""
'If Context Error':
'prefix': 'iec'
'body': """
if errors.Is(err, context.Canceled) {
$0
}
"""
'If Error in Test':
'prefix': 'iet'
'body': """
if err != nil {
t.Error(err)
}
"""
'If Fatal Error in Test':
'prefix': 'ief'
'body': """
if err != nil {
t.Fatal(err)
}
"""
'HTTP error':
'prefix': 'he'
'body': 'http.Error(w, http.StatusText(http.Status$1), http.Status$1)'
'Method':
'prefix': 'method'
'body': """
// $3 $4
func (${1:rec} *${2:T}) ${3:Name}(${4: int}) (err error) {
return ${5:code}
}
"""
'Function':
'prefix': 'fn'
'body': """
func ${1:name}()${2: int} {
return ${5:code}
}
"""
# 'Context Background':
# 'prefix': 'cb'
# 'body': 'context.Background()'
# 'Assert No Error':
# 'prefix': 'ane'
# 'body': """
# assert.NoError(t, ${1:err}, "$0")
# """
#
# 'Assert Equal':
# 'prefix': 'ae'
# 'body': """
# assert.Equal(t, ${1:expected}, ${2:actual})
# """
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment