当前位置:网站首页>How to use go code to compile Pb generated by proto file with protoc Compiler Go file

How to use go code to compile Pb generated by proto file with protoc Compiler Go file

2022-04-23 19:21:00 Handsome that handsome

package main
import (
	"fmt"
	"ProtocDemo/example"
	"github.com/golang/protobuf/proto"
	"os"
)
func main()  {
    
	fmt.Println("Hello World. \n")

	msg_test := &example.Person{
    
		Name: proto.String("Davie"),
		Age:  proto.Int(18),
		From: proto.String("China"),
	}

	// serialize 
	msgDataEncoding, err := proto.Marshal(msg_test)
	if err != nil {
    
		panic(err.Error())
		return
	}

	msgEntity := example.Person{
    }
	err = proto.Unmarshal(msgDataEncoding, &msgEntity)
	if err != nil {
    
		fmt.Println(err.Error())
		os.Exit(1)
		return
	}

	fmt.Printf(" full name :%s\n\n", msgEntity.GetName())
	fmt.Printf(" Age :%d\n\n", msgEntity.GetAge())
	fmt.Printf(" nationality :%s\n\n", msgEntity.GetFrom())
}

版权声明
本文为[Handsome that handsome]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/04/202204210557450913.html