Created
October 26, 2012 15:45
-
-
Save jordanorelli/3959521 to your computer and use it in GitHub Desktop.
compute a field in json output
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import ( | |
"encoding/json" | |
"os" | |
) | |
type Point struct { | |
X int `json:"x"` | |
Y int `json:"y"` | |
Z int `json:"z"` | |
} | |
func (p Point) MarshalJSON() ([]byte, error) { | |
type doc Point | |
d := doc(p) | |
d.Z = 3 | |
return json.Marshal(d) | |
} | |
func main() { | |
p := &Point{X: 1, Y: 2} | |
if err := json.NewEncoder(os.Stdout).Encode(p); err != nil { | |
panic(err) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment