package main
import (
"encoding/json"
"fmt"
"reflect"
"strconv"
)
type test struct {
Name string `json:"name" default:"bbc"`
Addr string `json:"addr"`
Port uint `json:"port" default:"88"`
User string `json:"user"`
Password string `json:"password"`
}
func MarshalJSON(i interface{}) ([]byte, error) {
typeof := reflect.TypeOf(i)
valueof := reflect.ValueOf(i)
for i := 0; i < typeof.Elem().NumField(); i++ {
if valueof.Elem().Field(i).IsZero() {
def := typeof.Elem().Field(i).Tag.Get("default")
if def != "" {
switch typeof.Elem().Field(i).Type.String() {
case "int":
result, _ := strconv.Atoi(def)
valueof.Elem().Field(i).SetInt(int64(result))
case "uint":
result, _ := strconv.ParseUint(def, 10, 64)
valueof.Elem().Field(i).SetUint(result)
case "string":
valueof.Elem().Field(i).SetString(def)
}
}
}
}
return json.Marshal(i)
}
func main() {
t := &test{
Addr: "0.0.0.0",
User: "localhost",
Password: "112233",
}
data, err := MarshalJSON(t)
if err != nil {
panic(err)
}
fmt.Println(string(data))
}