Object serialization: Difference between revisions

→‎{{header|Go}}: updates for two library changes
No edit summary
(→‎{{header|Go}}: updates for two library changes)
Line 524:
 
// serialize
f, err := os.OpenCreate("objects.dat", os.O_RDWR | os.O_CREAT | os.O_TRUNC, 0666)
if err != nil {
fmt.Println(err)
Line 540:
 
// read
f, err = os.Open("objects.dat", os.O_RDONLY, 0)
if err != nil {
fmt.Println(err)
Line 560:
 
type animal struct {
aliveAlive bool
}
 
func (a *animal) String() (r string) {
if a.aliveAlive {
r = "live"
} else {
Line 574:
type dog struct {
animal
obedienceTrainedObedienceTrained bool
}
 
func (d *dog) String() string {
r := "dog"
if !d.aliveAlive {
return "dead " + r
}
if d.obedienceTrainedObedienceTrained {
return "trained " + r
}
Line 590:
type cat struct {
animal
litterBoxTrainedLitterBoxTrained bool
}
 
func (c *cat) String() string {
r := "cat"
if !c.aliveAlive {
return "dead " + r
}
if c.litterBoxTrainedLitterBoxTrained {
return "litter box trained " + r
}
return r + ", not litter box trained"
}
 
type lab struct {
dog
colorColor string
}
 
func (l *lab) String() string {
r := "lab"
if l.colorColor == "" {
r += ", color unspecified"
} else {
r = l.colorColor + " lab"
}
if !l.aliveAlive {
return "dead " + r
}
if l.obedienceTrainedObedienceTrained {
return "trained " + r
}
Line 627:
type collie struct {
dog
catchesFrisbeeCatchesFrisbee bool
}
 
func (c *collie) String() string {
r := "collie"
if !c.aliveAlive {
return "dead " + r
}
switch {
case c.obedienceTrainedObedienceTrained && c.catchesFrisbeeCatchesFrisbee:
r = "trained " + r + ", catches frisbee"
case c.obedienceTrainedObedienceTrained && !c.catchesFrisbeeCatchesFrisbee:
r = "trained " + r + ", but doesn't catch frisbee"
case !c.obedienceTrainedObedienceTrained && c.catchesFrisbeeCatchesFrisbee:
r += ", not trained, but catches frisbee"
case !c.obedienceTrainedObedienceTrained && !c.catchesFrisbeeCatchesFrisbee:
r += ", not trained, doesn't catch frisbee"
}
1,707

edits