Skip to content

Instantly share code, notes, and snippets.

@jpillora
Last active August 29, 2015 14:16
Show Gist options
  • Save jpillora/46e378826f8f2e4d68a6 to your computer and use it in GitHub Desktop.
Save jpillora/46e378826f8f2e4d68a6 to your computer and use it in GitHub Desktop.
Sublime Auto-Complete Snippet - Go Class (Golang)
<snippet>
<name>Class</name>
<tabTrigger>class</tabTrigger>
<content><![CDATA[//${1:ClassName} is ${3:...}
type ${1:ClassName} struct {
${2}
}
//New${1:ClassName} creates a new ${1:ClassName}
func New${1:ClassName}(${2/\t?(\w+) ([\.\w\*]+)(\n)?/\l$1 $2,/mg}) *${1:ClassName} {
return &${1:ClassName}{
${2/(\w+) ([\.\w\*]+)/\t$1\: \l$1,/g}
}
}]]></content>
</snippet>
@jpillora
Copy link
Author

To install – Preferences > Browse Packages... > Go > Create a new class.sublime-snippet file

To use Type class TAB. Input the class name. TAB Input newline separated class fields. TAB Input a class comment. Will produce code like:

//Foo is an awesome class
type Foo struct {
    A string
    B bool
    C int
}

//NewFoo creates a new Foo
func NewFoo(a string, b bool, c int) *Foo {
    return &Foo{
        A: a,
        B: b,
        C: c,
    }
}

Note, since we can only have regex-ish features to make use of, we're left with a trailing comma in the arguments list. This is automatically cleared if you use goimports.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment