Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 12 additions & 5 deletions content/docs/py2go/module-04-structs-interfaces.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand All @@ -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"}
}
```
</UniversalEditor>
Expand Down
15 changes: 11 additions & 4 deletions content/docs/py2go/module-04-structs-interfaces.zh-cn.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand All @@ -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"}
}
```
</UniversalEditor>
Expand Down
15 changes: 11 additions & 4 deletions content/docs/py2go/module-04-structs-interfaces.zh-tw.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand All @@ -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"}
}
```
</UniversalEditor>
Expand Down