go struct 设置初始值

package main

import (

	"fmt"
	"github.com/mcuadros/go-defaults"
	"time"
)

type ExampleBasic struct {
	Foo bool   `default:"true"` //<-- StructTag with a default key
	Bar string `default:"33"`
	Qux int8 `default:"3"`
	Dur time.Duration `default:"1m"`
}

func NewExampleBasic() *ExampleBasic {
	example := new(ExampleBasic)
	defaults.SetDefaults(example) //<-- This set the defaults values

	return example
}

func main() {

	test := NewExampleBasic()
	fmt.Println(test.Foo) //Prints: true
	fmt.Println(test.Bar) //Prints: 33
	fmt.Println(test.Qux) //Prints:
	fmt.Println(test.Dur) //Prints: 1m0s

}

结果:

 这个包会有点问题:SetDefaults()函数会使用结构体中定义的值覆盖自己定义对象时初始化设置的值。其官网的例子存在问题。SetDefaults函数应该传入指针,并且Bar类型为string。

package main

import (

	"fmt"
	"github.com/mcuadros/go-defaults"
	"time"
)

type ExampleBasic struct {
	Foo bool   `default:"true"` //<-- StructTag with a default key
	Bar string `default:"33"`
	Qux int8 `default:"3"`
	Dur time.Duration `default:"1m"`
}

func NewExampleBasic() *ExampleBasic {
	example := new(ExampleBasic)
	defaults.SetDefaults(example) //<-- This set the defaults values

	return example
}

func main() {

	test := NewExampleBasic()
	fmt.Println(test.Foo) //Prints: true
	fmt.Println(test.Bar) //Prints: 33
	fmt.Println(test.Qux) //Prints: 3
	fmt.Println(test.Dur) //Prints: 1m0s

	fmt.Println("----------------------------------")
	example := ExampleBasic{
		Bar: "",
	}
	defaults.SetDefaults(&example)
	fmt.Printf("Bar :%s\n", example.Bar) //Prints: 33 instead of 0 (which is zero value for int)

	example.Bar = "" // set needed zero value AFTER applying defaults
	fmt.Printf("Bar :%s\n", example.Bar) //Prints: 0
}

结果:

也可以和json一起使用:

package main

import (
	"encoding/json"
	"fmt"
	"github.com/mcuadros/go-defaults"
	"time"
)

type ExampleBasic struct {
	Foo bool   `json:"foo" default:"true"` //<-- StructTag with a default key
	Bar string `json:"bar" default:"33"`
	Qux int8 `json:"qux" default:"3"`
	Dur time.Duration `json:"dur" default:"1m"`
}

func NewExampleBasic() *ExampleBasic {
	example := new(ExampleBasic)
	defaults.SetDefaults(example) //<-- This set the defaults values

	return example
}

func main() {

	test := NewExampleBasic()
	fmt.Println(test.Foo) //Prints: true
	fmt.Println(test.Bar) //Prints: 33
	fmt.Println(test.Qux) //Prints: 3
	fmt.Println(test.Dur) //Prints: 1m0s

	data, _ :=  json.Marshal(test)
	fmt.Println(string(data))
	fmt.Println("----------------------------------")
	example := ExampleBasic{
		Qux: 0,
	}
	fmt.Println(example.Qux) //Prints: 33 instead of 0 (which is zero value for int)

	defaults.SetDefaults(&example)
	fmt.Println(example.Qux) //Prints: 33 instead of 0 (which is zero value for int)

	example.Qux = 0 // set needed zero value AFTER applying defaults
	fmt.Println(example.Qux) //Prints: 0
}

结果:

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值