亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

grpc中能否傳輸context的內容?

grpc中能否傳輸context的內容?

Go
海綿寶寶撒 2023-04-24 16:17:25
我已經定義了一個客戶端和服務器。它們都有一個ctx(context)作為參數,client和server通過grpc進行通信。type Client interface {    GetFileVersion(ctx context.Context, in *Req) (*Resp, error)}type client struct {    cc *grpc.ClientConn}func (c *client) GetFileVersion(ctx context.Context, in *Req) (*Resp, error) {    out := new(Resp)    err := grpc.Invoke(ctx, "/testpb/GetFileVersion", in, out, c.cc, opts...)    if err != nil {        return nil, err    }    return out, nil}type Server interface {    GetFileVersion(context.Context, *Req) (*Resp, error)}如果我在客戶端實現GetFileVersion時在ctx中設置值,我可以在服務器端獲取到這個值嗎?ctx = context.WithValue(parent, "test", 123)
查看完整描述

1 回答

?
搖曳的薔薇

TA貢獻1793條經驗 獲得超6個贊

context是全局請求變量的通用注冊表。它不應該逃脫請求范圍。考慮換一種方式。



查看完整回答
反對 回復 2023-04-24
  • 1 回答
  • 0 關注
  • 183 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號