Henge is a type conversion library for Golang.
変化 (Henge) means "Appearing with a different figure." in Japanese.
Henge as the name implies can easily convert to different types.
- 💫 Easily converting to various types
- int64, uint64, float64, bool, string, slice, map, and struct.
- ⚡ Simple and minimal code.
- 🔧 Support for custom conversions by callbacks before and after conversion.
In Golang world, there is a trade-off between pointer and non-pointer type, so it is used both as needed.
For the reason, it is often necessary to convert between pointers and non-pointers.
When using Henge, it can easily convert even if it's a struct field.
There are many cases where the API server response is not the same as the DB record.
Henge is very useful if you just want to copy, but want to ignore some fields.
If we try to assign a non-zero constant value to a String or Int pointer type, we need to write codes of multiple lines.
When using Henge, it easy to create pointer types while preserving the benefits of types.
To install it, run:
go get -u github.com/soranoba/henge/v2
import (
"fmt"
"github.com/soranoba/henge/v2"
)
func main() {
type UserRequest struct {
Name *string
Age *int
}
type User struct {
Name string // *string to string
Age string // *int to string
}
name := "Alice"
age := 30
var user User
if err := henge.New(UserRequest{Name: &name, Age: &age}).Convert(&user); err != nil {
return
}
fmt.Printf("%#v", user)
}
import (
"fmt"
"github.com/soranoba/henge/v2"
)
func main() {
i, err := henge.New("1.25").Float().Int().Result()
if err != nil {
return
}
// "1.25" -> 1.25 -> 1
fmt.Println(i)
}