diff --git a/content/docs/py2go/module-04-structs-interfaces.mdx b/content/docs/py2go/module-04-structs-interfaces.mdx index c1116f2..55c595f 100644 --- a/content/docs/py2go/module-04-structs-interfaces.mdx +++ b/content/docs/py2go/module-04-structs-interfaces.mdx @@ -171,13 +171,20 @@ import ( "fmt" ) +// encoding/json tag options (after the field name, comma-separated): +// omitempty - omit the field when it holds the zero value +// string - marshal numbers/bools as JSON strings +// omitzero - omit the field when zero (Go 1.24+) +// "-" - never include this field +// Unknown options are silently ignored, so typos won't produce errors. + type User struct { ID int `json:"id"` // JSON key Name string `json:"name"` // JSON key - Email string `json:"email,omitempty"` // Omit if empty + Email string `json:"email,omitempty"` // Field name + omitempty option Password string `json:"-"` // Never in JSON - Internal string `json:"internal,internal"` // Multiple options - DBName string `db:"user_name" json:"name"` // Multiple tags + Internal string `json:"internal,omitempty"` // Field name + omitempty option + DBName string `db:"user_name" json:"db_name"` // Multiple tags } func main() { @@ -186,14 +193,14 @@ func main() { Name: "Alice", Email: "alice@example.com", Password: "secret", - Internal: "internal_data", + Internal: "", DBName: "alice_db", } // JSON encoding respects tags jsonBytes, _ := json.Marshal(user) fmt.Println(string(jsonBytes)) - // Output: {"id":1,"name":"Alice","email":"alice@example.com","internal":"internal_data"} + // Output: {"id":1,"name":"Alice","email":"alice@example.com"} } ``` diff --git a/content/docs/py2go/module-04-structs-interfaces.zh-cn.mdx b/content/docs/py2go/module-04-structs-interfaces.zh-cn.mdx index 4a9ae4e..4f696ee 100644 --- a/content/docs/py2go/module-04-structs-interfaces.zh-cn.mdx +++ b/content/docs/py2go/module-04-structs-interfaces.zh-cn.mdx @@ -171,13 +171,20 @@ import ( "fmt" ) +// encoding/json 标签选项(在字段名之后,用逗号分隔): +// omitempty - 当字段为零值时省略该字段 +// string - 将数字/布尔值编码为 JSON 字符串 +// omitzero - 当字段为零时省略(Go 1.24+) +// "-" - 完全忽略该字段 +// 未知选项会被静默忽略,因此拼写错误不会报错。 + type User struct { ID int `json:"id"` // JSON 键 Name string `json:"name"` // JSON 键 Email string `json:"email,omitempty"` // 如果为空则省略 Password string `json:"-"` // 从不在 JSON 中 - Internal string `json:"internal,internal"` // 多个选项 - DBName string `db:"user_name" json:"name"` // 多个标签 + Internal string `json:"internal,omitempty"` // 字段名 + omitempty 选项 + DBName string `db:"user_name" json:"db_name"` // 多个标签 } func main() { @@ -186,14 +193,14 @@ func main() { Name: "Alice", Email: "alice@example.com", Password: "secret", - Internal: "internal_data", + Internal: "", DBName: "alice_db", } // JSON 编码会遵守标签 jsonBytes, _ := json.Marshal(user) fmt.Println(string(jsonBytes)) - // 输出: {"id":1,"name":"Alice","email":"alice@example.com","internal":"internal_data"} + // 输出: {"id":1,"name":"Alice","email":"alice@example.com"} } ``` diff --git a/content/docs/py2go/module-04-structs-interfaces.zh-tw.mdx b/content/docs/py2go/module-04-structs-interfaces.zh-tw.mdx index 5fae904..a69636c 100644 --- a/content/docs/py2go/module-04-structs-interfaces.zh-tw.mdx +++ b/content/docs/py2go/module-04-structs-interfaces.zh-tw.mdx @@ -171,13 +171,20 @@ import ( "fmt" ) +// encoding/json 標籤選項(在欄位名稱之後,用逗號分隔): +// omitempty - 當欄位為零值時省略該欄位 +// string - 將數字/布林值編碼為 JSON 字串 +// omitzero - 當欄位為零時省略(Go 1.24+) +// "-" - 完全忽略該欄位 +// 未知選項會被靜默忽略,因此拼寫錯誤不會報錯。 + type User struct { ID int `json:"id"` // JSON 鍵 Name string `json:"name"` // JSON 鍵 Email string `json:"email,omitempty"` // 如果為空則省略 Password string `json:"-"` // 從不在 JSON 中 - Internal string `json:"internal,internal"` // 多個選項 - DBName string `db:"user_name" json:"name"` // 多個標籤 + Internal string `json:"internal,omitempty"` // 欄位名稱 + omitempty 選項 + DBName string `db:"user_name" json:"db_name"` // 多個標籤 } func main() { @@ -186,14 +193,14 @@ func main() { Name: "Alice", Email: "alice@example.com", Password: "secret", - Internal: "internal_data", + Internal: "", DBName: "alice_db", } // JSON 編碼會遵守標籤 jsonBytes, _ := json.Marshal(user) fmt.Println(string(jsonBytes)) - // 輸出: {"id":1,"name":"Alice","email":"alice@example.com","internal":"internal_data"} + // 輸出: {"id":1,"name":"Alice","email":"alice@example.com"} } ```