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.
if err != nil {
fmt.Println(err)
Line 540:
// read
f, err = os.Open("objects.dat"
if err != nil {
fmt.Println(err)
Line 560:
type animal struct {
}
func (a *animal) String() (r string) {
if a.
r = "live"
} else {
Line 574:
type dog struct {
animal
}
func (d *dog) String() string {
r := "dog"
if !d.
return "dead " + r
}
if d.
return "trained " + r
}
Line 590:
type cat struct {
animal
}
func (c *cat) String() string {
r := "cat"
if !c.
return "dead " + r
}
if c.
return "litter box trained " + r
}
return r + ", not litter box trained"
}
type lab struct {
dog
}
func (l *lab) String() string {
r := "lab"
if l.
r += ", color unspecified"
} else {
r = l.
}
if !l.
return "dead " + r
}
if l.
return "trained " + r
}
Line 627:
type collie struct {
dog
}
func (c *collie) String() string {
r := "collie"
if !c.
return "dead " + r
}
switch {
case c.
r = "trained " + r + ", catches frisbee"
case c.
r = "trained " + r + ", but doesn't catch frisbee"
case !c.
r += ", not trained, but catches frisbee"
case !c.
r += ", not trained, doesn't catch frisbee"
}
|