This Visual Studio Code extension streamlines the generation of boilerplate code for Go structures.
It focuses on trivial code generation tasks like getters and setters, constructors, and interface stubs. More complicated tasks, such as handling nested struct, are out of scope for this extension.
This extension is inspired by Language Support for Java(TM) by Red.
There are multiple ways to trigger the code generation. For example, you can use Source Actions
and select the desired action from the list:
Alternatively, you can use the command palette (e.g., Go: Generate Getters and Setter
), or by right-clicking on the editor.
impl command has to be installed for "Gererate Interface Stub" feature.
Generates getters and setters for the fields of the structure with nil protection.
type Foo struct {
bar int
}
// following methods are generated
func (f *Foo) GetBar() int {
if f != nil {
return f.bar
}
return 0
}
func (f *Foo) SetBar(bar int) {
if f != nil {
f.bar = bar
}
}
If no nil protection is needed, you can disable it in vscode settings.
"go.sourceAction.accessor.nilProtection": false
Note: This feature is enabled by default.
// following methods are generated
func (f *Foo) GetBar() int {
return f.bar
}
func (f *Foo) SetBar(bar int) {
f.bar = bar
}
Generates constructors for the structure.
type Foo struct {
bar int
}
// following methods are generated
func NewFoo(bar int) *Foo {
return &Foo{
bar: bar,
}
}
Generates an interface stub for the structure.
type Foo struct {
bar int
}
// Foo implements io.Closer
func (f *Foo) Close() error {
panic("not implemented") // TODO: Implement
}
This feature is originally provided by vscode-go extension, and I made a tiny modification to omit the need to explicitly specify the receiver and struct name.
This extension contributes the following settings:
go.sourceAction.accessor.nilProtection
: enable/disable nil protection for getters and setters, see Generate Getters and Setters for more details.go.sourceAction.accessor.defaultValueForType
: user specified default value for types. When getters have nil receiver, this value is returned if the type of the field matches this config.
For example, if you don't want to return ""
for string fields, you can set this config like below.
"go.sourceAction.accessor.defaultValueForType": {
"string": "consts.EmptyString",
}
then the generated code will look like this.
func (f *Foo) GetBar() int {
if f != nil {
return f.bar
}
return consts.EmptyString
}
This project is licensed under the GPLv3 license
see GPLv3.0