Go json.MarshalJS JSON.stringify
Returns([]byte, error)string
Error handlingSecond return valueThrows exception
Field visibilityExported fields only (capitalized)All enumerable properties
Field namingStruct 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
}