写点什么

Go 中如何使用结构体标签

用户头像
baiyutang
关注
发布于: 刚刚
Go 中如何使用结构体标签

译者:baiyutang

原文:https://www.digitalocean.com/community/tutorials/how-to-use-struct-tags-in-go


介绍


结构体标签看起来是什么


type User struct {    Name string `example:"name"`}
复制代码


package main
import "fmt"
type User struct { Name string `example:"name"`}
func (u *User) String() string { return fmt.Sprintf("Hi! My name is %s", u.Name)}
func main() { u := &User{ Name: "Sammy", }
fmt.Println(u)}
复制代码

它会输出

OutputHi! My name is Sammy
复制代码

JSON 编码

{  "language": "Go",  "mascot": "Gopher"}
复制代码


package main
import ( "encoding/json" "fmt" "log" "os" "time")
type User struct { Name string Password string PreferredFish []string CreatedAt time.Time}
func main() { u := &User{ Name: "Sammy the Shark", Password: "fisharegreat", CreatedAt: time.Now(), }
out, err := json.MarshalIndent(u, "", " ") if err != nil { log.Println(err) os.Exit(1) }
fmt.Println(string(out))}
复制代码

这会打印如下输出


{  "Name": "Sammy the Shark",  "Password": "fisharegreat",  "CreatedAt": "2019-09-23T15:50:01.203059-04:00"}
复制代码


package main
import ( "encoding/json" "fmt" "log" "os" "time")
type User struct { name string password string preferredFish []string createdAt time.Time}
func main() { u := &User{ name: "Sammy the Shark", password: "fisharegreat", createdAt: time.Now(), }
out, err := json.MarshalIndent(u, "", " ") if err != nil { log.Println(err) os.Exit(1) }
fmt.Println(string(out))}
复制代码


使用结构体标签控制编码


package main
import ( "encoding/json" "fmt" "log" "os" "time")
type User struct { Name string `json:"name"` Password string `json:"password"` PreferredFish []string `json:"preferredFish"` CreatedAt time.Time `json:"createdAt"`}
func main() { u := &User{ Name: "Sammy the Shark", Password: "fisharegreat", CreatedAt: time.Now(), }
out, err := json.MarshalIndent(u, "", " ") if err != nil { log.Println(err) os.Exit(1) }
fmt.Println(string(out))}
复制代码

这会输出

{  "name": "Sammy the Shark",  "password": "fisharegreat",  "preferredFish": null,  "createdAt": "2019-09-23T18:16:17.57739-04:00"}
复制代码

移出结构体空字段

package main
import ( "encoding/json" "fmt" "log" "os" "time")
type User struct { Name string `json:"name"` Password string `json:"password"` PreferredFish []string `json:"preferredFish,omitempty"` CreatedAt time.Time `json:"createdAt"`}
func main() { u := &User{ Name: "Sammy the Shark", Password: "fisharegreat", CreatedAt: time.Now(), }
out, err := json.MarshalIndent(u, "", " ") if err != nil { log.Println(err) os.Exit(1) }
fmt.Println(string(out))}
复制代码

这个例子将输出

{  "name": "Sammy the Shark",  "password": "fisharegreat",  "createdAt": "2019-09-23T18:21:53.863846-04:00"}
复制代码

忽略私有字段

package main
import ( "encoding/json" "fmt" "log" "os" "time")
type User struct { Name string `json:"name"` Password string `json:"-"` CreatedAt time.Time `json:"createdAt"`}
func main() { u := &User{ Name: "Sammy the Shark", Password: "fisharegreat", CreatedAt: time.Now(), }
out, err := json.MarshalIndent(u, "", " ") if err != nil { log.Println(err) os.Exit(1) }
fmt.Println(string(out))}
复制代码


当你运营这个例子,你将看到这个输出:

{  "name": "Sammy the Shark",  "createdAt": "2019-09-23T16:08:21.124481-04:00"}
复制代码

总结


发布于: 刚刚阅读数: 4
用户头像

baiyutang

关注

广州 2017.12.13 加入

Microservices | Golang | Cloud Nitive | “Smart work,Not hard”

评论

发布
暂无评论
Go 中如何使用结构体标签