https://github.com/karitham/go-genial
A golang code-generator helper.
https://github.com/karitham/go-genial
codegen golang
Last synced: 9 months ago
JSON representation
A golang code-generator helper.
- Host: GitHub
- URL: https://github.com/karitham/go-genial
- Owner: karitham
- License: mit
- Created: 2021-11-02T21:40:14.000Z (over 4 years ago)
- Default Branch: master
- Last Pushed: 2022-01-28T14:51:53.000Z (over 4 years ago)
- Last Synced: 2025-07-11T14:27:35.108Z (12 months ago)
- Topics: codegen, golang
- Language: Go
- Homepage:
- Size: 24.4 KB
- Stars: 7
- Watchers: 1
- Forks: 1
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# go-genial
[](https://pkg.go.dev/github.com/Karitham/go-genial)
A golang code-generation library
## Install
`go get github.com/karitham/go-genial`
## Example
```go
p := &genial.PackageB{}
p.Comment("example is an example package").
Name("example").
Imports("encoding/json")
t := &genial.StructB{}
t.Comment("Baz is a implementation of Iface").
Name("Baz").
Field("Foo", "*string", genial.StructTag{Type: "json", Value: "foo,omitempty"}).
Field("rest", "json.Raw")
f := &genial.FuncB{}
f.Comment("FooBar is a new example function").
Name("FooBar").
Receiver("b", "*Baz").
Parameter("foo", "int").
Parameter("bar", "string").
ReturnTypes("int", "error").
WriteString(`panic("not implemented")`)
i := &genial.InterfaceB{}
i.Comment("Iface is an example interface").
Members(f).
Name("Iface")
p.Declarations(t, i, f).WriteTo(os.Stdout)
```
generates
```go
// example is an example package
package example
import "encoding/json"
// Baz is a implementation of Iface
type Baz struct {
Foo *string `json:"foo,omitempty"`
rest json.Raw
}
// Iface is an example interface
type Iface interface {
// FooBar is a new example function
FooBar(foo int, bar string) (int, error)
}
// FooBar is a new example function
func (b *Baz) FooBar(foo int, bar string) (int, error) {
panic("not implemented")
}
```