Go json.Marshal | JS JSON.stringify | |
|---|---|---|
| Returns | ([]byte, error) | string |
| Error handling | Second return value | Throws exception |
| Field visibility | Exported fields only (capitalized) | All enumerable properties |
| Field naming | Struct tags (json:"name") | Property names as-is |
Go-specific behaviour
Only exported (capitalized) fields are marshaled:
type Example struct {
Name string // ✅ marshaled
age int // ❌ ignored
}Control JSON output with struct tags:
type User struct {
Name string `json:"name"`
Age int `json:"age,omitempty"` // omit if zero
}