goaccessor
is a Go tool designed to automate the generation of getter
and setter
boilerplate code for your types and variables.
go install github.com/yujiachen-y/goaccessor@latest
After installing goaccessor
, you can use it in the command line interface (CLI) or with //go:generate
directives. Check out the Examples section for more information.
You can find a runnable 'book' example in the example folder.
Consider the file book.go
:
package main
//go:generate goaccessor --target Book --getter --setter
type Book struct {
Title string
Author string
}
When we run go generate
, it creates a new file book_goaccessor.go
:
// Code generated by "goaccessor --target Book --getter --setter". DO NOT EDIT.
package main
func (b *Book) GetTitle() string {
return b.Title
}
func (b *Book) SetTitle(title string) {
b.Title = title
}
func (b *Book) GetAuthor() string {
return b.Author
}
func (b *Book) SetAuthor(author string) {
b.Author = author
}
goaccessor
isn't just for struct types; it can also handle top-level constants and variables. For instance:
//go:generate goaccessor --target books --getter --setter
var books = map[string]*Book {
...
}
After executing go generate
, we get:
func GetBooks() map[string]*Book {
return books
}
func SetBooks(newBooks map[string]*Book) {
books = newBooks
}
In certain cases, you might want to export specific fields of a top-level variable with a prefix:
//go:generate goaccessor --target bestSellingBook --field --getter --include Author --prefix BestSelling
var bestSellingBook = &Book{ ... }
This directive will generate:
func GetBestSellingAuthor() string {
return bestSellingBook.Author
}
Here are the available options for goaccessor
:
Option | Short option | Description |
---|---|---|
--target | -t | Specify the target to be handled. |
--getter | -g | Generate getter for the target. |
--setter | -s | Generate setter for the target. |
--accessor | -a | Generate both getter and setter for the target. |
--pure-getter | -pg | Generate getter without 'Get' prefix for the target. |
--prefix | -p | Add a prefix to the generated methods/functions. |
--field | -f | Apply the flag (getter , setter , accessor ) to each field of the target (only applicable for struct type variables). |
--include | -i | Generate methods only for the specified fields (fields should be comma-separated). |
--exclude | -e | Exclude specified fields from method generation (fields should be comma-separated). |
Remember, when using the --pure-getter
option, the generated getter methods won't have a 'Get' prefix.
For instance, for a Book
struct with a Title
field, the getter will be Title()
instead of GetTitle()
.
If you do not want to install goaccessor
and want to use it as a dependency for your project, follow these steps:
- Go to your project directory and add the
goaccessor
dependency viago mod
:
go get github.com/yujiachen-y/goaccessor@latest
- Create a new file named
tools.go
(or any other name you like) with the following code:
//go:build tools
package main
import (
_ "github.com/yujiachen-y/goaccessor"
)
-
If you want to use the
goaccessor
CLI command, usego run github.com/yujiachen-y/goaccessor@latest
instead. -
If you use
go:generate
directives to generate code, change the directives like this:
//go:generate go run github.com/yujiachen-y/goaccessor@latest -target book
var book Book
- Note: even though
goaccessor
is a dependency of your project, it will not—and should not—be a part of your project's build result. We use a build flag intools.go
to ensuregoaccessor
is ignored during build.