| // +build OMIT |
| |
| package main |
| |
| import ( |
| "encoding/binary" |
| "io" |
| "log" |
| "os" |
| ) |
| |
| type Gopher struct { |
| Name string |
| AgeYears int |
| } |
| |
| func (g *Gopher) WriteTo(w io.Writer) (size int64, err error) { |
| err = binary.Write(w, binary.LittleEndian, int32(len(g.Name))) |
| if err != nil { |
| return |
| } |
| size += 4 |
| n, err := w.Write([]byte(g.Name)) |
| size += int64(n) |
| if err != nil { |
| return |
| } |
| err = binary.Write(w, binary.LittleEndian, int64(g.AgeYears)) |
| if err == nil { |
| size += 4 |
| } |
| return |
| } |
| |
| func main() { |
| g := &Gopher{ |
| Name: "Gophertiti", |
| AgeYears: 3382, |
| } |
| |
| if _, err := g.WriteTo(os.Stdout); err != nil { |
| log.Printf("DumpBinary: %v\n", err) |
| } |
| } |