网络编程
位置:首页>> 网络编程>> Go语言>> golang gorm 结构体的表字段缺省值设置方式

golang gorm 结构体的表字段缺省值设置方式

作者:wide288  发布时间:2024-04-28 09:13:53 

标签:golang,gorm,表字段

我就废话不多说了,大家还是直接看代码吧~


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}

以上为个人经验,希望能给大家一个参考,也希望大家多多支持asp之家。如有错误或未考虑完全的地方,望不吝赐教。

来源:https://blog.csdn.net/wide288/article/details/100814933

0
投稿

猜你喜欢

手机版 网络编程 asp之家 www.aspxhome.com