本文整理汇总了Golang中goprotobuf/googlecode/com/hg/proto.Marshal函数的典型用法代码示例。如果您正苦于以下问题:Golang Marshal函数的具体用法?Golang Marshal怎么用?Golang Marshal使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了Marshal函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Golang代码示例。
示例1: main
func main() {
m1 := &test.Member{
Name: proto.String("Linus"),
Age: proto.Int32(99),
}
m1.Skills = []test.Skill{test.Skill_ASM, test.Skill_C}
m1_data, err := proto.Marshal(m1)
if err != nil {
log.Fatal("marshaling error: ", err)
os.Exit(1)
}
fmt.Println("m1: ", m1)
fmt.Println("m1.Skills: ", m1.Skills)
fmt.Println("actual data: ", m1_data)
println("=== unmarshalling m1 into m2 ===")
m2 := &test.Member{}
proto.Unmarshal(m1_data, m2)
fmt.Println("m2: ", m2)
port := 9090
fmt.Println("=== END EXAMPLES / SERVER STARTING at %d ===", port)
service := fmt.Sprintf(":%d", port)
tcpAddr, err := net.ResolveTCPAddr("ip4", service)
checkError(err)
listener, err := net.ListenTCP("tcp", tcpAddr)
checkError(err)
for {
conn, err := listener.Accept()
checkError(err)
input := make([]byte, 1024)
n, err := conn.Read(input)
input = input[:n]
fmt.Println("input: ", input)
club := &test.Club{}
err = proto.Unmarshal(input, club)
checkError(err)
sum := int32(0)
fmt.Println("len: ", len(club.Member))
for _, m := range club.Member {
sum += *m.Age
}
avg := float64(sum) / float64(len(club.Member))
fmt.Printf("avg age: %f (sum: %d)\n", avg, sum)
reply := &test.Reply{Average: proto.Float64(avg)}
out_data, err := proto.Marshal(reply)
conn.Write(out_data) // don't care about return value
conn.Close() // we're finished with this client
}
}
示例2: writeT
func (c *conn) writeT(t *T) os.Error {
if c.err != nil {
return c.err
}
buf, err := pb.Marshal(t)
if err != nil {
return err
}
c.err = binary.Write(c.c, binary.BigEndian, int32(len(buf)))
if c.err != nil {
return c.err
}
for len(buf) > 0 {
n, err := c.c.Write(buf)
if err != nil {
c.err = err
return err
}
buf = buf[n:]
}
return nil
}
示例3: TestMarshalUnmarshalBase64
func TestMarshalUnmarshalBase64(t *testing.T) {
var encbuf []byte
var decbuf []byte
t.Logf("start with buf %s\n", proto.CompactTextString(&cr))
t.Log("marshalling protobuf")
buf, err := proto.Marshal(&cr)
if err != nil {
t.Error("marshal error: ", err)
}
t.Log("marshalled")
t.Log("urlencoding")
t.Logf("need %d size buffer\n", base64.URLEncoding.EncodedLen(len(buf)-1))
t.Log(buf)
t.Logf("%v %s\n", buf, buf)
encbuf = make([]byte, base64.URLEncoding.EncodedLen(len(buf)), base64.URLEncoding.EncodedLen(len(buf)))
base64.URLEncoding.Encode(encbuf, buf)
t.Log("urlencoded")
t.Log("urldecoding")
t.Logf("need %d size buffer\n", base64.URLEncoding.DecodedLen(len(encbuf)))
t.Logf("%v %s\n", encbuf, encbuf)
decbuf = make([]byte, base64.URLEncoding.DecodedLen(len(encbuf)), base64.URLEncoding.DecodedLen(len(encbuf)))
n, err := base64.URLEncoding.Decode(decbuf, encbuf)
t.Logf("wrote %d bytes from encbuf to decbuf. len(encbuf)=%d, len(buf)=%d\n", n, len(encbuf), len(buf))
if err != nil {
t.Error("urldecode error: ", err)
}
t.Log("urldecoded")
t.Log(buf, decbuf)
rcr := &CheckResult{}
t.Log("unmarshalling")
err = proto.Unmarshal(decbuf, rcr)
t.Logf("%s\n", proto.CompactTextString(rcr))
}
示例4: mustMarshal
func mustMarshal(p interface{}) []byte {
buf, err := proto.Marshal(p)
if err != nil {
panic(err)
}
return buf
}
示例5: broadcast
func (r *run) broadcast(m *msg) {
if m != nil {
m.Seqn = &r.seqn
b, _ := proto.Marshal(m)
for _, addr := range r.addr {
r.out <- Packet{addr, b}
}
}
}
示例6: respond
func (c *conn) respond(t *T, flag int32, cc chan bool, r *R) {
r.Tag = t.Tag
r.Flags = pb.Int32(flag)
tag := pb.GetInt32(t.Tag)
if flag&Done != 0 {
c.closeTxn(tag)
}
if c.poisoned {
select {
case cc <- true:
default:
}
return
}
buf, err := pb.Marshal(r)
c.wl.Lock()
defer c.wl.Unlock()
if err != nil {
c.poisoned = true
select {
case cc <- true:
default:
}
log.Println(err)
return
}
err = binary.Write(c.c, binary.BigEndian, int32(len(buf)))
if err != nil {
c.poisoned = true
select {
case cc <- true:
default:
}
log.Println(err)
return
}
for len(buf) > 0 {
n, err := c.c.Write(buf)
if err != nil {
c.poisoned = true
select {
case cc <- true:
default:
}
log.Println(err)
return
}
buf = buf[n:]
}
}
示例7: DoMarshalling
func DoMarshalling(value interface{}) (key string, data []byte, err os.Error) {
data, err = proto.Marshal(value)
if err == nil {
h := sha256.New()
nr, err := h.Write([]byte(data))
if err == nil && nr == len([]byte(data)) {
key = hex.EncodeToString(h.Sum())
}
}
return
}
示例8: SendProtobuf
func SendProtobuf(writer io.Writer, pb interface{}) os.Error {
var data []byte
var err os.Error
if data, err = proto.Marshal(pb); err != nil {
return err
}
size := uint64(len(data))
binary.Write(writer, binary.LittleEndian, size)
_, err = writer.Write(data)
return err
}
示例9: Call
func (c *context) Call(service, method string, in, out interface{}) os.Error {
data, err := proto.Marshal(in)
if err != nil {
return err
}
res, err := call(service, method, data)
if err != nil {
return err
}
return proto.Unmarshal(res, out)
}
示例10: Encode
// Encode returns an opaque representation of the key
// suitable for use in HTML and URLs.
// This is compatible with the Python and Java runtimes.
func (k *Key) Encode() string {
ref := keyToProto("", k)
b, err := proto.Marshal(ref)
if err != nil {
panic(err)
}
// Trailing padding is stripped.
return strings.TrimRight(base64.URLEncoding.EncodeToString(b), "=")
}
示例11: TestMarshalUnmarshal
func TestMarshalUnmarshal(t *testing.T) {
t.Log("marshalling protobuf")
buf, err := proto.Marshal(&cr)
if err != nil {
log.Fatal("marshal error: ", err)
}
t.Log("marshalled")
rcr := new(CheckResult)
t.Log("unmarshalling")
err = proto.Unmarshal(buf, rcr)
t.Log(rcr)
}
示例12: newTestMessage
func newTestMessage() *pb.MyMessage {
msg := &pb.MyMessage{
Count: proto.Int32(42),
Name: proto.String("Dave"),
Quote: proto.String(`"I didn't want to go."`),
Pet: []string{"bunny", "kitty", "horsey"},
Inner: &pb.InnerMessage{
Host: proto.String("footrest.syd"),
Port: proto.Int32(7001),
Connected: proto.Bool(true),
},
Others: []*pb.OtherMessage{
&pb.OtherMessage{
Key: proto.Int64(0xdeadbeef),
Value: []byte{1, 65, 7, 12},
},
&pb.OtherMessage{
Weight: proto.Float32(6.022),
Inner: &pb.InnerMessage{
Host: proto.String("lesha.mtv"),
Port: proto.Int32(8002),
},
},
},
Bikeshed: pb.NewMyMessage_Color(pb.MyMessage_BLUE),
Somegroup: &pb.MyMessage_SomeGroup{
GroupField: proto.Int32(8),
},
// One normally wouldn't do this.
// This is an undeclared tag 13, as a varint (wire type 0) with value 4.
XXX_unrecognized: []byte{13<<3 | 0, 4},
}
ext := &pb.Ext{
Data: proto.String("Big gobs for big rats"),
}
if err := proto.SetExtension(msg, pb.E_Ext_More, ext); err != nil {
panic(err)
}
// Add an unknown extension. We marshal a pb.Ext, and fake the ID.
b, err := proto.Marshal(&pb.Ext{Data: proto.String("3G skiing")})
if err != nil {
panic(err)
}
b = append(proto.EncodeVarint(104<<3|proto.WireBytes), b...)
proto.SetRawExtension(msg, 104, b)
// Extensions can be plain fields, too, so let's test that.
b = append(proto.EncodeVarint(105<<3|proto.WireVarint), 19)
proto.SetRawExtension(msg, 105, b)
return msg
}
示例13: sendProtoMessage
// Send a protobuf-encoded message
func (c *Client) sendProtoMessage(kind uint16, msg interface{}) (err os.Error) {
d, err := proto.Marshal(msg)
if err != nil {
return
}
c.msgchan <- &Message{
buf: d,
kind: kind,
}
return
}
示例14: PrintLocationsAsProto
func PrintLocationsAsProto(locations map[string]Location) []byte {
locProto := &locationProto.LocationInfo{
make([]*locationProto.Location, len(locations)), nil}
loc := locProto.Location
for name, location := range locations {
loc[0] = &locationProto.Location{
proto.String(name), proto.Float64(float64(location.lat)), proto.Float64(float64(location.lng)),
nil, proto.Float64(float64(location.accuracy)), proto.Int64(location.timestamp), nil}
loc = loc[1:]
}
data, _ := proto.Marshal(locProto)
return data
}
示例15: UpdatePlayerCoordinate
func (this *PushProtoHandler) UpdatePlayerCoordinate(x, y int) {
fmt.Printf("Updating player coordinate\n")
m := NewUpdatePlayerCoord()
m.Coord = new(Coordinate)
m.Coord.X = proto.Int32(int32(x))
m.Coord.Y = proto.Int32(int32(y))
data, err := proto.Marshal(m)
if err != nil {
fmt.Printf("E: %s", err)
return
}
this.Proxy.SendMsg(data, PORT_PUSH, Server_UPDATELOCATION, false)
}