Advertisement
Guest User

Untitled

a guest
Jul 28th, 2015
223
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.00 KB | None | 0 0
  1. package main
  2.  
  3. import (
  4. "log"
  5.  
  6. "github.com/golang/protobuf/proto"
  7.  
  8. "github.com/example"
  9. )
  10.  
  11. /*
  12. sudo apt-get install protobuf-compiler
  13. go get -u github.com/golang/protobuf/{proto,protoc-gen-go}
  14. cd /home/ubuntu/go/src/github.com/example
  15. protoc --go_out=. *.proto
  16.  
  17. go run main.go
  18. */
  19.  
  20. func main() {
  21.  
  22. test := &example.Test{
  23. Label: proto.String("hello"),
  24. Type: proto.Int32(17),
  25. Optionalgroup: &example.Test_OptionalGroup{
  26. RequiredField: proto.String("good bye"),
  27. },
  28. }
  29. data, err := proto.Marshal(test)
  30. if err != nil {
  31. log.Fatal("marshaling error: ", err)
  32. }
  33. newTest := &example.Test{}
  34. err = proto.Unmarshal(data, newTest)
  35. if err != nil {
  36. log.Fatal("unmarshaling error: ", err)
  37. }
  38. // Now test and newTest contain the same data.
  39. if test.GetLabel() != newTest.GetLabel() {
  40. log.Fatalf("data mismatch %q != %q", test.GetLabel(), newTest.GetLabel())
  41. }
  42.  
  43. log.Printf("Unmarshalled to: %+v", newTest)
  44. // 2015/07/09 00:58:49 Unmarshalled to: label:"hello" type:17 OptionalGroup{RequiredField:"good bye" }
  45. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement