Last active
August 29, 2015 14:16
-
-
Save dpetersen/b68669c256935217c4ba to your computer and use it in GitHub Desktop.
Serialize shmerialize
This file contains 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" | |
"fmt" | |
) | |
type Cat struct { | |
Angry bool `json:"angry"` | |
Declawed bool `json:"declawed"` | |
} | |
type CatThreatAssessment struct { | |
Cat | |
IsDangerous bool `json:"is_dangerous"` | |
} | |
func NewCatThreatAssessment(c Cat) CatThreatAssessment { | |
return CatThreatAssessment{ | |
Cat: c, | |
IsDangerous: c.Angry && !c.Declawed, | |
} | |
} | |
func main() { | |
dangerousCat := Cat{Angry: true, Declawed: false} | |
justAngryCat := Cat{Angry: true, Declawed: true} | |
dcj, _ := json.MarshalIndent(NewCatThreatAssessment(dangerousCat), "", " ") | |
jacj, _ := json.MarshalIndent(NewCatThreatAssessment(justAngryCat), "", " ") | |
fmt.Printf("dangerousCat JSON:\n%v\n", string(dcj)) | |
fmt.Printf("justAngryCat JSON:\n%v\n", string(jacj)) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Output: