You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
But why repeated string --> []string instead of repeated string --> []*string ?
My user case is here:
I'm using reflect to set value to Message struct, roughly like this:
v := reflect.ValueOf(msg).Elem()
switch v.Field(i).Interface().(type) {
case *bool:
var tmpBool bool
cmd.Flags().BoolVarP(&tmpBool, name, shorthand, bVal, usage)
v.Field(i).Set(reflect.ValueOf(&tmpBool))
case []string:
var tmpSliceString []string
cmd.Flags().StringSliceVarP(&tmpSliceString, name, shorthand, defaultValue, usage)
v.Field(i).Set(reflect.ValueOf(tmpSliceString)) //set the SliceFlag here
...
My message struct is below:
type Test struct {
BoolFlag *bool `protobuf:"varint,1,opt,name=BoolFlag" json:"BoolFlag,omitempty"`
StringFlag *string `protobuf:"bytes,2,opt,name=StringFlag" json:"StringFlag,omitempty"`
Int32Flag *int32 `protobuf:"varint,3,opt,name=Int32Flag" json:"Int32Flag,omitempty"`
SliceFlag []string `protobuf:"bytes,4,rep,name=SliceFlag" json:"SliceFlag,omitempty"`
XXX_unrecognized []byte `json:"-"`
}
the value tmpBool is currently only initial but it may be changed in other places, so pass a *bool can make my message perceive the changes, but []string without pointer only copy the value which initial just empty, but that value may changed, but message can't perceive that.
is there any way to generate []*string maybe ? if not, is there any workaround here?
Thanks
The text was updated successfully, but these errors were encountered:
I've find a workaround here using an extra message which can pass by reference instead of value
message Slice {
repeated string value = 1;
}
case *cmdproto.Slice:
switch ext {
case cmdproto.FlagDetail_ISSTRSLICE:
var tmpSliceString cmdproto.Slice
cmd.Flags().StringSliceVarP(&tmpSliceString.Value, name, shorthand, defaultValue, usage)
v.Field(i).Set(reflect.ValueOf(&tmpSliceString))
but still wondering why not repeated string --> []*string
shiywang
changed the title
Scalar Value Types Problems why not []*string
Scalar Value Types Problems repeated string --> why not []*string pass by reference
Oct 30, 2017
The proto2 format allows for optional values. Since Go lacks optional types or unions types, it resorted to using pointers to indicate the difference between a set zero value and an unset value. As for slices, there is no need to make that distinction.
Furthermore, I don't think it's feasible to add an option just to allow generating []*string instead of []string.
according to scalar value types in golang
But why
repeated string --> []string
instead ofrepeated string --> []*string
?My user case is here:
I'm using reflect to set value to
Message
struct, roughly like this:the value
tmpBool
is currently only initial but it may be changed in other places, so pass a*bool
can make my message perceive the changes, but[]string
without pointer only copy the value which initial just empty, but that value may changed, but message can't perceive that.is there any way to generate []*string maybe ? if not, is there any workaround here?
Thanks
The text was updated successfully, but these errors were encountered: