https://github.com/ttakuya50/goor
This is a CLI for creating constructors, getters and setters for structures, and is only for Golang.
https://github.com/ttakuya50/goor
constructor generator getter go golang setter
Last synced: 27 days ago
JSON representation
This is a CLI for creating constructors, getters and setters for structures, and is only for Golang.
- Host: GitHub
- URL: https://github.com/ttakuya50/goor
- Owner: ttakuya50
- License: bsd-3-clause
- Created: 2020-05-22T08:07:14.000Z (over 5 years ago)
- Default Branch: master
- Last Pushed: 2020-06-16T10:38:15.000Z (over 5 years ago)
- Last Synced: 2024-06-22T12:52:21.895Z (over 1 year ago)
- Topics: constructor, generator, getter, go, golang, setter
- Language: Go
- Homepage: https://pkg.go.dev/github.com/ttakuya50/goor
- Size: 12.7 KB
- Stars: 2
- Watchers: 2
- Forks: 1
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# goor
This is a CLI for creating constructors, getters and setters for structures, and is only for Golang.
# Installation
```shell script
$ go get -u github.com/ttakuya50/goor
```
If you want the direct binary data, please click [here](https://github.com/ttakuya50/goor/releases).
# Usage
```shell script
Usage of goor:
goor [flags] -type=[struct type name]
For more information, see:
https://github.com/ttakuya50/goor
Flags:
-getter
[option] when you create a constructor, you also create a getter.
-output string
[option] output file name (default "srcdir/_constructor_gen.go").
-pointer
[option] set the return value to a pointer when creating the constructor. (default true)
-setter
[option] when you create a constructor, you also create a setter.
-type string
[required] a struct type name.
-version
outputs the current version.
```
Frequently used settings are listed below.
## 1. Create constructor.
```go
//go:generate goor -type=User
type User struct {
id int
name string
age int
}
```
```shell script
$ go generate ./...
```
A file named user_constructor_gen.go will be created.
Constructor is generated.
```go
func NewUser(
id int,
name string,
age age,
) *User {
return &User{
id: id,
name: name,
age: age,
}
}
```
## 2. Create constructor and getter.
```go
//go:generate goor -type=User -getter
type User struct {
id int
name string
age int
}
```
```shell script
$ go generate ./...
```
A file named user_constructor_gen.go will be created.
Constructor and getter are generated.
```go
func NewUser(
id int,
name string,
age int,
) *User {
return &User{
id: id,
name: name,
age: age,
}
}
func (g *User) Id() int {
return g.id
}
func (g *User) Name() string {
return g.name
}
func (g *User) Age() int {
return g.age
}
```
## 3. Create constructor and getter and setter.
```go
//go:generate goor -type=User -getter -setter
type User struct {
id int
name string
age int
}
```
```shell script
$ go generate ./...
```
A file named user_constructor_gen.go will be created.
Constructor and getter and setter are generated.
```go
func NewUser(
id int,
name string,
age int,
) *User {
return &User{
id: id,
name: name,
age: age,
}
}
func (g *User) Id() int {
return g.id
}
func (g *User) Name() string {
return g.name
}
func (g *User) Age() int {
return g.age
}
func (s *User) SetId(id int) {
s.id = id
}
func (s *User) SetName(name string) {
s.name = name
}
func (s *User) SetAge(age int) {
s.age = age
}
```
## 4. It is also possible to exclude certain structure variables.
```go
//go:generate goor -type=User
type User struct {
id int `goor:"constructor:-"`
name string
age int
}
```
```shell script
$ go generate ./...
```
It is also possible to exclude certain structure variables.
```go
func NewUser(
name string,
age int,
) *User {
return &User{
name: name,
age: age,
}
}
```
## 5. It is also possible to exclude the constructor, getter and setter of a specific structure variable.
```go
//go:generate goor -type=User -getter -setter
type User struct {
id int `goor:"getter:-"`
name string `goor:"constructor:-;setter:-"`
age int `goor:"constructor:-;getter:-;setter:-"`
}
```
```shell script
$ go generate ./...
```
The following code will be generated on its own.
```go
func NewUser(
id int,
) *User {
return &User{
id: id,
}
}
func (g *User) Name() string {
return g.name
}
func (s *User) SetId(id int) {
s.id = id
}
```
## 5. Change the name of the output file.
```go
//go:generate goor -type=User -output=user_gen.go
type User struct {
id int
name string
age int
}
```
```shell script
$ go generate ./...
```
The following code will be generated in the file named user_gen.go
```go
func NewUser(
id int,
name string,
age int,
) *User {
return &User{
id: id,
name: name,
age: age,
}
}
```
# Examples
Check out the examples of how to generate code using goor in the examples directory.
# Author
[t_takuya50](https://twitter.com/t_takuya50)