我就废话不多说了,大家还是直接看代码吧~
type animal struct {
id int64
name string `gorm:"default:'galeone'"`
age int64
}
把 name 设置上缺省值 galeone 了。
补充:golang 巧用构造函数设置结构体的默认值
看代码吧~
package main
import "fmt"
type s1 struct {
id string
s2 s2
s3 s3
}
type s2 struct {
websitename string
url string
}
type s3 struct {
keyword []string
where string
}
func main() {
ss := s1{
id: "123456",
s2: s2{
websitename: "ydook.com",
url: "www.ydook.com",
},
s3: s3{
// 重点:在结构体内部使用数组
keyword: []string{"it", "ai", "web", "technology", "knowledge"},
where: "it",
},
}
fmt.println(ss)
}
运行结果:
main.somestruct
{16881699 www.ydook.com}
以上为个人经验,希望能给大家一个参考,也希望大家多多支持。如有错误或未考虑完全的地方,望不吝赐教。