GO结构定义中的字符串文字 [英] String literals in GO structure definition
问题描述
在此结构定义中:
type API struct {
Message string "json:message"
}
字符串 "json:message" 的含义是什么,以及如何访问它. 预先谢谢你.
what is the meaning of the string "json:message" and how to access it if it is accessible. Thank you in advance.
推荐答案
这些是struct标记.此struct标记由包使用
encoding/json
到Marshal
对象转换为JSON,Unmarshal
JSON字符串转换为对象
These are struct tags. This struct tag is used by package
encoding/json
to Marshal
objects to JSON and Unmarshal
JSON string to objects
在将一个结构封送(编码)为JSON字符串时,它将寻找该struct标记以分配JSON
密钥名称,如果不存在,则可能使用该结构字段名称本身
while marshaling (encoding ) a struct to JSON string it will look for this struct tag to assign JSON
key name, if not present it may use the struct field name itself
btw语法错误,必须是
btw the syntax is wrong it has to be
type API struct {
Message string `json:"message"`
}
这是示例程序供参考 https://play.golang.org/p/FsMGNuDB8P
这篇关于GO结构定义中的字符串文字的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!