gob2.go 792 B

123456789101112131415161718192021222324252627282930313233343536373839
  1. // gob2.go
  2. package main
  3. import (
  4. "encoding/gob"
  5. "log"
  6. "os"
  7. )
  8. type Address struct {
  9. Type string
  10. City string
  11. Country string
  12. }
  13. type VCard struct {
  14. FirstName string
  15. LastName string
  16. Addresses []*Address
  17. Remark string
  18. }
  19. var content string
  20. func main() {
  21. pa := &Address{"private", "Aartselaar","Belgium"}
  22. wa := &Address{"work", "Boom", "Belgium"}
  23. vc := VCard{"Jan", "Kersschot", []*Address{pa,wa}, "none"}
  24. // fmt.Printf("%v: \n", vc) // {Jan Kersschot [0x126d2b80 0x126d2be0] none}:
  25. // using an encoder:
  26. file, _ := os.OpenFile("vcard.gob", os.O_CREATE|os.O_WRONLY, 0666)
  27. defer file.Close()
  28. enc := gob.NewEncoder(file)
  29. err := enc.Encode(vc)
  30. if err != nil {
  31. log.Println("Error in encoding gob")
  32. }
  33. }