第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號(hào)安全,請及時(shí)綁定郵箱和手機(jī)立即綁定
已解決430363個(gè)問題,去搜搜看,總會(huì)有你想問的

reflect.Select 函數(shù)是需要為什么?我認(rèn)為這是因?yàn)橥ǖ辣仨毷欠瓷渲担?/h1>

Go
UYOU 2022-01-17 18:17:32

在什么情況下會(huì)需要reflect.Select?我找到了例子,但 Select 的使用似乎是做作的。任何示例,其中 reflect.Select 比正常選擇是必要的?
查看完整描述

2 回答

?
侃侃無極

TA貢獻(xiàn)2051條經(jīng)驗(yàn) 獲得超10個(gè)贊

最近有一篇來自 MongoDB 的人的文章。顯然,此代碼在其實(shí)用mongodump程序中用于生產(chǎn)。


使用的具體代碼reflect.Select如下所示(來自文章):


// Run multiplexer until it receives an EOF on the control channel.

func (mux *Multiplexer) Run() {

    for {

        index, value, recv := reflect.Select(mux.selectCases)

        EOF := !recv

        // note that the control channel is always at index 0

        if index == 0 {

            if EOF {

                return

            }

            muxInput, _ := value.Interface().(*muxInputSource)

            mux.selectCases = append(mux.selectCases, reflect.SelectCase{

                Dir:  reflect.SelectRecv,

                Chan: reflect.ValueOf(muxInput.collection),

                Send: reflect.Value{},

            })

        } else {

            if EOF {

                mux.writeEOF()

                mux.selectCases = append(mux.selectCases[:index], mux.selectCases[index+1:]...)

            } else {

                document, _ := value.Interface().([]byte)

                mux.writeDocument(document)[]

            }

        }

    }

}

我認(rèn)為他們使用reflect.Select而不是直接的原因select:


goroutine 的數(shù)量(以及通道的數(shù)量)是在運(yùn)行時(shí)確定的(使用-j標(biāo)志)。事實(shí)上,它似乎隨著append. (歸功于@cnicutar 的評論)

通道的類型在運(yùn)行時(shí)確定。這允許他們的muxInput.collection類型是任何它想要的。(歸功于@JimB 的評論)


查看完整回答
反對 回復(fù) 2022-01-17
?
桃花長相依

TA貢獻(xiàn)1860條經(jīng)驗(yàn) 獲得超8個(gè)贊

如果您的頻道是固定的,您可以使用選擇觀看它們;但是,如果您的頻道不固定并且您想觀看它們,則可以使用 reflect.Select。


有一些代碼片段


var (

      events  []reflect.SelectCase

      err     error

      lenHdr  = 3 // zk conn event chan, shutdown chan, children refresh chan

      lenCW   = len(z.ChildWatches)    // a channel slice:chan ChildWatche

      lenDW   = len(z.DataWatches)     //a channel slice:chan DataWatche

      lenEN   = len(z.EphemeralNodes)  //a channel slice: chan EphemeralNode

  )

  events = make([]reflect.SelectCase, lenHdr+lenCW+lenDW+lenEN)

  events[0] = reflect.SelectCase{Dir: reflect.SelectRecv, Chan: reflect.ValueOf(FreshChanConEvent())} // kick off the event loop

  events[1] = reflect.SelectCase{Dir: reflect.SelectRecv, Chan: reflect.ValueOf(z.shutdownChan)}

  events[2] = reflect.SelectCase{Dir: reflect.SelectRecv, Chan: reflect.ValueOf(z.refreshChildChan)}

  for i, _ := range z.ChildWatches {

      events[lenHdr+i] = reflect.SelectCase{Dir: reflect.SelectRecv, Chan: reflect.Zero(reflect.TypeOf((chan zk.Event)(nil)))}

  }

  for i, _ := range z.DataWatches {

      events[lenHdr+lenCW+i] = reflect.SelectCase{Dir: reflect.SelectRecv, Chan: reflect.Zero(reflect.TypeOf((chan zk.Event)(nil)))}

  }

  for i, info := range z.EphemeralNodes {

    events[lenHdr+lenCW+lenDW+i] = reflect.SelectCase{Dir: reflect.SelectRecv, Chan: reflect.ValueOf(info.Ch)}

  }


  for {

    chosen, value, ok := reflect.Select(events)

    if chosen == xxxxx {...}

這段代碼是看頻道數(shù)組(z.ChildWatches、z.DataWatches、z.EphemeralNodes)


查看完整回答
反對 回復(fù) 2022-01-17
  • 2 回答
  • 0 關(guān)注
  • 288 瀏覽

添加回答

了解更多

舉報(bào)

0/150
提交
取消
微信客服

購課補(bǔ)貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動(dòng)學(xué)習(xí)伙伴

公眾號(hào)

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號(hào)