README ¶
getter
Generate getters for golang using tags.
Similar project:
By default, getters for all private fields will be generated. To ignore, add get:"-"
tag.
Installation
$ go install github.com/alextanhongpin/getter
Using CLI
$ getter \
-in=path/to/your/input/file.go \
-type=YourStructType1,YourStructType2 \ # Accepts multiple struct names.
-out=optional/path/to/your/output/dir/or/file.go \ # Generated based on struct name with _gen.go suffix if not provided.
-prefix=OptionalPrefix # Adds a custom prefix, e.g. Get, by default no prefix will be added.
Using go generate
When using go generate
, the input path is inferred automatically.
package examples
import "github.com/google/uuid"
//go:generate getter -type User
type User struct {
id uuid.UUID
name string `get:"FullName"`
remarks *string `get:"-"`
acc Account `get:",inline,Account"`
}
//go:generate getter -type Account
type Account struct {
id int64
name string
}
Basic Usage
Using the following example, running go generate
will create two new files, user_gen.go
and account_gen.go
respectively in the same directory the command is ran.
package examples
import "github.com/google/uuid"
//go:generate getter -type User
type User struct {
id uuid.UUID
name string `get:"FullName"`
remarks *string `get:"-"`
acc Account `get:",inline,Account"`
// Public field will not be generated.
Version int
}
//go:generate getter -type Account
type Account struct {
id int64
name string
}
user_gen.go
// Code generated by github.com/alextanhongpin/getter, DO NOT EDIT.
package examples
import uuid "github.com/google/uuid"
func (u User) ID() uuid.UUID {
return u.id
}
func (u User) FullName() string {
return u.name
}
func (u User) AccountID() int64 {
return u.acc.id
}
func (u User) AccountName() string {
return u.acc.name
}
account_gen.go
// Code generated by github.com/alextanhongpin/getter, DO NOT EDIT.
package examples
func (a Account) ID() int64 {
return a.id
}
func (a Account) Name() string {
return a.name
}
Adding Prefix
By default, the name generated will be the upper common initialism, e.g. (id
becomes ID
instead of Id
, url
becomes URL
etc). If you wish to customize the prefix, you can provide the -prefix
option.
package examples
import "github.com/google/uuid"
//go:generate go run ../main.go -type User -prefix Get
type User struct {
id uuid.UUID
name string `get:"FullName"`
remarks *string `get:"-"`
acc Account `get:",inline,Account"`
}
//go:generate go run ../main.go -type Account -prefix Get
type Account struct {
id int64
name string
}
user_gen.go
package examples
import uuid "github.com/google/uuid"
func (u User) GetID() uuid.UUID {
return u.id
}
func (u User) GetFullName() string {
return u.name
}
func (u User) GetAccountID() int64 {
return u.acc.id
}
func (u User) GetAccountName() string {
return u.acc.name
}
account_gen.go
// Code generated by github.com/alextanhongpin/getter, DO NOT EDIT.
package examples
func (a Account) GetID() int64 {
return a.id
}
func (a Account) GetName() string {
return a.name
}
Inline vs Non-inline
Non-Inline
package examples
import "github.com/google/uuid"
//go:generate go run ../main.go -type User -prefix Get
type User struct {
id uuid.UUID
name string `get:"FullName"`
remarks *string `get:"-"`
acc Account
}
//go:generate go run ../main.go -type Account -prefix Get
type Account struct {
id int64
name string
}
Output:
// Code generated by github.com/alextanhongpin/getter, DO NOT EDIT.
package examples
import uuid "github.com/google/uuid"
func (u User) GetID() uuid.UUID {
return u.id
}
func (u User) GetFullName() string {
return u.name
}
func (u User) GetAcc() Account {
return u.acc
}
Inline
package examples
import "github.com/google/uuid"
//go:generate go run ../main.go -type User -prefix Get
type User struct {
id uuid.UUID
name string `get:"FullName"`
remarks *string `get:"-"`
// NOTE: You can also give the inlined getter a prefix,
// otherwise the generated field will be ID() and Name(),
// which will collide with the user's field.
acc Account `get:",inline,Account"`
}
//go:generate go run ../main.go -type Account -prefix Get
type Account struct {
id int64
name string
}
Output:
// Code generated by github.com/alextanhongpin/getter, DO NOT EDIT.
package examples
import uuid "github.com/google/uuid"
func (u User) GetID() uuid.UUID {
return u.id
}
func (u User) GetFullName() string {
return u.name
}
func (u User) GetAccountID() int64 {
return u.acc.id
}
func (u User) GetAccountName() string {
return u.acc.name
}
Single-file output
When generating getters for multiple structs, you may end up generating one file per struct. To consolidate the generated files into a single file, provide multiple types in a single command.
package examples
import "github.com/google/uuid"
/*
You can provide the list of structs as comma separated list:
go:generate go run ../main.go -type User,Account
Or by declaring -type multiple times, which may be useful if the list gets longer.
go:generate go run ../main.go -type User -type Account
*/
//go:generate go run ../main.go -type User,Account
type User struct {
id uuid.UUID
name string `get:"FullName"`
remarks *string `get:"-"`
acc Account `get:",inline,Account"`
}
type Account struct {
id int64
name string
}
basic_gen.go
The generated file will then contain the getters for both `User` and `Account`.
// Code generated by github.com/alextanhongpin/getter, DO NOT EDIT.
package examples
import uuid "github.com/google/uuid"
func (a Account) ID() int64 {
return a.id
}
func (a Account) Name() string {
return a.name
}
func (u User) ID() uuid.UUID {
return u.id
}
func (u User) FullName() string {
return u.name
}
func (u User) AccountID() int64 {
return u.acc.id
}
func (u User) AccountName() string {
return u.acc.name
}
Features
- generate getters with custom prefix
- tags to ignore field, rename field, inline struct, add inline prefix
Documentation ¶
There is no documentation for this package.