美文网首页Go Go
GRPC oneof client参数请求

GRPC oneof client参数请求

作者: wuhan_goer | 来源:发表于2021-05-25 19:49 被阅读0次

    Oneof Fields

    For this message with a oneof field:

    package account;
    message Profile {
      oneof avatar {
        string image_url = 1;
        bytes image_data = 2;
      }
    }
    

    the compiler generates the structs:

    type Profile struct {
            // Types that are valid to be assigned to Avatar:
            //      *Profile_ImageUrl
            //      *Profile_ImageData
            Avatar isProfile_Avatar `protobuf_oneof:"avatar"`
    }
    
    type Profile_ImageUrl struct {
            ImageUrl string
    }
    type Profile_ImageData struct {
            ImageData []byte
    }
    

    Both *Profile_ImageUrl and *Profile_ImageData implement isProfile_Avatar by providing an empty isProfile_Avatar() method.

    The following example shows how to set the field:

    p1 := &account.Profile{
      Avatar: &account.Profile_ImageUrl{"http://example.com/image.png"},
    }
    
    // imageData is []byte
    imageData := getImageData()
    p2 := &account.Profile{
      Avatar: &account.Profile_ImageData{imageData},
    }
    

    相关文章

      网友评论

        本文标题:GRPC oneof client参数请求

        本文链接:https://www.haomeiwen.com/subject/mcvcsltx.html