Hello! This tool is a tool that provides similar functions to Lombok, one of the Java Libraries, in Golang. Using this tool, you can easily create and use frequently used utility functions such as Getter, Setter, Constructor, Builder, etc. in Golang using comment annotations.
Type the following command in the terminal.
$ go install github.com/YangTaeyoung/[email protected]
- Go to the folder to scan for annotations.
cd <path>
- Assume you have the following file.
// in ./test/test.go // @Builder type Test struct { Name string Age int }
- Run the following command.
$ gombok
- gombok scans all Go files in the
test
folder, finds annotations, and creates the following file.// in ./test/test_gombok.go // DO NOT EDIT THIS FILE. THIS FILE WAS AUTO GENERATED BY GOMBOK. package test // TestBuilder is a builder for Test type TestBuilder struct { target *Test } // SetName sets the Name field of the target Test func (tb TestBuilder) WithName(name string) TestBuilder { tb.target.Name = name return tb } // SetAge sets the Age field of the target Test func (tb TestBuilder) WithAge(age int) TestBuilder { tb.target.Age = age return tb } // Build constructs a Test from the builder func (tb TestBuilder) Build() Test { return *tb.target } // NewTestBuilder creates a new builder instance for Test func NewTestBuilder() TestBuilder { return TestBuilder{target: &Test{}} }
- Now you can use the easily created function as follows.
func SomeMethod() { test := NewTestBuilder().WithName("Yang").WithAge(25).Build() }
Annotation | Description |
---|---|
@AllArgsConstructor |
Creates a Constructor that takes all parameters. |
@NoArgsConstructor |
Creates a Constructor that takes no parameters. |
@RequiredArgsConstructor |
Creates a Constructor that takes only fields with the validate:"required" tag. |
@Builder |
Creates a Builder. |
@Getter |
Creates a Getter. |
@Setter |
Creates a Setter. |
@ToString |
Creates a ToString() function. |
@Equals |
Creates an Equals() function. |
// @{Constructor Annotation}
can be used to make the constructor the default constructor New()
of the package.
// some_file.go
// Test
// @AllArgsConstructor.Default
type Test struct {
Name string
Age int
}
// some_file_gombok.go
// New
func New(name string, age int) Test {
return Test{
Name: name,
Age: age,
}
}
다음 태그를 이용하여 gombok을 통해 생성되는 함수의 동작을 변경할 수 있습니다.
Tag | Value | Description |
---|---|---|
validate |
required |
Specifies the fields created by the @RequiredArgsConstructor annotation. |
constructor |
ignore |
The field with this tag is excluded from the Constructor created by the @AllArgsConstructor and @RequiredArgsConstructor annotations. |
builder |
ignore |
The WithXXX() method is not created in the Builder created by the @Builder annotation for the field with this tag. |
builder |
must |
If the field with this tag is zero value or nil for pointer type, panic is raised in the WithXXX() method created by the @Builder annotation. |
getter |
ignore |
The Getter method of the corresponding field created by the @Getter annotation is not created for the field with this tag. |
setter |
ignore |
The Setter method of the corresponding field created by the @Setter annotation is not created for the field with this tag. |
to_string |
ignore |
The String() method created by the @ToString annotation is excluded from the field with this tag. |
$ gombok
> zsh: command not found: gombok
error that occurs when running a program installed with go.
Add the environment variable as follows at the bottom of the ~/.zshrc
file (or ~/.bashrc
).
# ...
export PATH="$HOME/go/bin:$PATH"