main.go 1.1 KB

12345678910111213141516171819202122232425262728293031323334
  1. package main
  2. import (
  3. "fmt"
  4. "google.golang.org/protobuf/proto"
  5. )
  6. func main() {
  7. s1 := &Student{} //��һ��ѧ����Ϣ
  8. s1.Name = "jz01"
  9. s1.Age = 23
  10. s1.Address = "cq"
  11. s1.Cn = ClassName_class2 //ö�����͸�ֵ
  12. ss := &Students{}
  13. ss.Person = append(ss.Person, s1) //����һ��ѧ����Ϣ���ӵ�Students��Ӧ����Ƭ��
  14. s2 := &Student{} //�ڶ���ѧ����Ϣ
  15. s2.Name = "jz02"
  16. s2.Age = 25
  17. s2.Address = "cd"
  18. s2.Cn = ClassName_class3
  19. ss.Person = append(ss.Person, s2) //���ڶ���ѧ����Ϣ���ӵ�Students��Ӧ����Ƭ��
  20. ss.School = "cqu"
  21. fmt.Println("Students��ϢΪ��", ss)
  22. // Marshal takes a protocol buffer message
  23. // and encodes it into the wire format, returning the data.
  24. buffer, _ := proto.Marshal(ss)
  25. fmt.Println("���л�֮�����ϢΪ��", buffer)
  26. // Use UnmarshalMerge to preserve and append to existing data.
  27. data := &Students{}
  28. proto.Unmarshal(buffer, data)
  29. fmt.Println("�����л�֮�����ϢΪ��", data)
  30. }