json snake case tag generater.
$ go get github.com/yudppp/json_snake_case/cmd/json_snake_case
package main
import (
"encoding/json"
"fmt"
)
type User struct {
UserID int
Name string
}
func main() {
user := User{UserID: 10, Name: "yudppp"}
b, _ := json.Marshal(&user)
fmt.Println(string(b))
}
the above print {"UserID":10,"Name":"yudppp"}
$ cd path/to/app
$ json_snake_case -type=User
Generated user_json.go
file.
And go run again. This print {"user_id":10,"name":"yudppp"}
...
//go:generate json_snake_case -type=User
type User struct {
UserID int
Name string
}
...
$ go generate
type User struct {
UserID int
Name string
}
// -->
type UserJSON struct {
UserID int `json:"user_id"`
Name string `json:"name"`
}
type User struct {
UserID int `json:"-"`
Name string `json:"nickname"`
Email *string `json:",omitempty"`
}
// -->
type UserJSON struct {
UserID int `json:"-"`
Name string `json:"nickname"`
Email *string `json:"email,omitempty"`
}
- add test code