FAQ
All GO generated code use pointers (ptr) inside struct for a protobuf
message. However, the reflect.Value in reflect package doesn't have a
SetPtr function. So there is no way to set the field values inside a proto
message using 'reflect'?

For example, the proto definition is :

message ArpEntry { // required, ip address in string format. e.g. “198.32.176.123”. optional string ip_addr = 1; // required, physical address in byte string. e.g. “(\300\332\374(\245”. optional string phy_addr = 2; // optional, interface index. One of "if_index" or "if_descr" is required. optional int32 if_index = 3; // optional, interface description optional string if_descr = 4;}


The generated GO code is:
type ArpEntry struct {
// required, ip address in string format. e.g. “198.32.176.123”.
IpAddr *string `protobuf:"bytes,1,opt,name=ip_addr"
json:"ip_addr,omitempty"`
// required, physical address in byte string. e.g. “(\300\332\374(\245”.
PhyAddr *string `protobuf:"bytes,2,opt,name=phy_addr"
json:"phy_addr,omitempty"`
// optional, interface index. One of "if_index" or "if_descr" is required.
IfIndex *int32 `protobuf:"varint,3,opt,name=if_index"
json:"if_index,omitempty"`
// optional, interface description
IfDescr *string `protobuf:"bytes,4,opt,name=if_descr"
json:"if_descr,omitempty"`
XXX_unrecognized []byte `json:"-"`
}

The question is: how can I use 'reflect' to set value for a field such as
'IfIndex'?
If IfIndex is int32, I could use SetInt. But there is no SetPtr?

arpEntry := &data_proto.ArpEntry{IpAddr:nil, PhyAddr:nil, IfIndex:nil,
IfDescr:nil}
arpEntryElem := reflect.ValueOf(arpEntry).Elem()
field := arpEntryElem.FieldByName("IfIndex")
// Then how to set IfIndex? it is a ptr.

--
You received this message because you are subscribed to the Google Groups "golang-nuts" group.
To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts+unsubscribe@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Search Discussions

  • David Symonds at Jul 10, 2014 at 3:44 am

    On 10 July 2014 03:56, wrote:

    arpEntry := &data_proto.ArpEntry{IpAddr:nil, PhyAddr:nil, IfIndex:nil,
    IfDescr:nil}
    arpEntryElem := reflect.ValueOf(arpEntry).Elem()
    field := arpEntryElem.FieldByName("IfIndex")
    // Then how to set IfIndex? it is a ptr.
    Call its Set method. You might want reflect.New to get a fresh thing
    of the right type.

    --
    You received this message because you are subscribed to the Google Groups "golang-nuts" group.
    To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts+unsubscribe@googlegroups.com.
    For more options, visit https://groups.google.com/d/optout.

Related Discussions

Discussion Navigation
viewthread | post
Discussion Overview
groupgolang-nuts @
categoriesgo
postedJul 10, '14 at 3:37a
activeJul 10, '14 at 3:44a
posts2
users2
websitegolang.org

2 users in discussion

Wyao: 1 post David Symonds: 1 post

People

Translate

site design / logo © 2021 Grokbase