DEV Community

masaushi
masaushi

Posted on • Updated on

[golang] Generating getter/setter methods easily

Hi all,
I have made a go tool to generate accessor methods(getter or setter) for unexported struct fields, so let me introduce it.

https://github.com/masaushi/accessory

GitHub logo masaushi / accessory

accessor methods generator for Go programming language

accessory

test release

accessory is an accessor generator for Go programming language.

What is accessory?

Accessory is a tool that generates accessor methods from any structs.

Sometimes you might make struct fields unexported in order for values of fields not to be accessed or modified from anywhere in your codebases, and define getters or setters for values to be handled in a desired way.

But writing accessors for so many fields is time-consuming, but not exciting or creative.

Accessory frees you from tedious, monotonous tasks.

Installation

To get the latest released version

Go version < 1.16

go get github.com/masaushi/accessory
Enter fullscreen mode Exit fullscreen mode

Go 1.16+

go install github.com/masaushi/accessory@latest
Enter fullscreen mode Exit fullscreen mode

Usage

Declare Struct with accessor Tag

accessory generates accessor methods from defined structs, so you need to declare a struct and fields with accessor tag.

Values for accessor tag is getter and setter, getter is for generating getter method and setter is for setter methods.

Here…

Sometimes you might make struct fields unexported in order for values of the fields not to be accessed or modified from anywhere in your codebases.

Making struct fields unexported will prevent data from unexpected use or change.
But you sometimes might need accessor methods for some fields to get/set data in a desired way.

However, writing accessors for many fields is time-consuming, but not exciting or creative.

This tool frees you from that tedious, monotonous task.

Example

1. Declare a model with accessor tag.
type MyStruct struct {
    field1 string    `accessor:"getter"`
    field2 *int      `accessor:"setter"`
    field3 time.Time `accessor:"getter,setter"`
}
Enter fullscreen mode Exit fullscreen mode
2. Run accessory command
$ accessory -type MyStruct .
Enter fullscreen mode Exit fullscreen mode
3. Accessor methods will be generated.
func(m *MyStruct) Field1() string {
    return m.field1
}

func(m *MyStruct) SetField2(val *int) {
    m.field2 = val
}

func(m *MyStruct) Field3() time.Time {
    return m.field3
}

func(m *MyStruct) SetField3(val time.Time) {
    m.field3 = val
}
Enter fullscreen mode Exit fullscreen mode
4. You can customize method name
type MyStruct struct {
    field1 string `accessor:"getter:GetFirstField"`
    field2 int    `accessor:"setter:ChangeSecondField"`
}
Enter fullscreen mode Exit fullscreen mode

Generated methods will be

func(m *MyStruct) GetFirstField() string {
    return m.field1
}

func(m *MyStruct) ChangeSecondField(val *int) {
    m.field2 = val
}
Enter fullscreen mode Exit fullscreen mode
5. You can also generate accessors with go generate
//go:generate accessory -type MyStruct

type MyStruct struct {
    field1 string `accessor:"getter"`
    field2 *int   `accessor:"setter"`
}
Enter fullscreen mode Exit fullscreen mode

Conclusion

Generating accessors easily will save your time and help you focus on writing important business logics.

I'm looking forward to getting your feedback.

Have a wonderful day!

Top comments (0)