今天就跟大家聊聊有關(guān)使用golang怎么對gorm結(jié)構(gòu)體的字段進(jìn)行設(shè)置,可能很多人都不太了解,為了讓大家更加了解,小編給大家總結(jié)了以下內(nèi)容,希望大家根據(jù)這篇文章可以有所收獲。
type Animal struct { ID int64 Name string `gorm:"default:'galeone'"` Age int64 }
把 name 設(shè)置上缺省值 galeone 了。
補(bǔ)充:Golang 巧用構(gòu)造函數(shù)設(shè)置結(jié)構(gòu)體的默認(rèn)值
看代碼吧~
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{ // 重點(diǎn):在結(jié)構(gòu)體內(nèi)部使用數(shù)組 KeyWord: []string{"IT", "AI", "Web", "technology", "knowledge"}, Where: "IT", }, } fmt.Println(ss) }
運(yùn)行結(jié)果:
main.SomeStruct
{16881699 www.ydook.com}
看完上述內(nèi)容,你們對使用golang怎么對gorm結(jié)構(gòu)體的字段進(jìn)行設(shè)置有進(jìn)一步的了解嗎?如果還想了解更多知識或者相關(guān)內(nèi)容,請關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道,感謝大家的支持。