<kbd id="afajh"><form id="afajh"></form></kbd>
<strong id="afajh"><dl id="afajh"></dl></strong>
    <del id="afajh"><form id="afajh"></form></del>
        1. <th id="afajh"><progress id="afajh"></progress></th>
          <b id="afajh"><abbr id="afajh"></abbr></b>
          <th id="afajh"><progress id="afajh"></progress></th>

          Go1.18 這個(gè)包確定沒(méi)了

          共 2614字,需瀏覽 6分鐘

           ·

          2022-02-10 22:28

          閱讀本文大概需要 5 分鐘。

          大家好,我是 polarisxu。

          Go1.18 已經(jīng)發(fā)布 Beta2 版本了,正式版本預(yù)計(jì) 3 月份發(fā)布。Go1.18 最重要的特性莫過(guò)于泛型,之前寫(xiě)過(guò)幾篇相關(guān)文章:

          其中提到一個(gè)標(biāo)準(zhǔn)庫(kù)新包:constraints,相關(guān)提案見(jiàn):https://github.com/golang/go/issues/45458。該包的目的是想預(yù)定義一些常用的泛型約束,避免用戶自己重復(fù)定義。

          這是該包最初希望包含的約束:

          //?Package?constraints?defines?a?set?of?useful?constraints?to?be?used?with?type?parameters.
          package?constraints

          //?Signed?is?a?constraint?that?permits?any?signed?integer?type.
          type?Signed?interface?{?...?}

          //?Unsigned?is?a?constraint?that?permits?any?unsigned?integer?type.
          type?Unsigned?interface?{?...?}

          //?Integer?is?a?constraint?that?permits?any?integer?type.
          type?Integer?interface?{?...?}

          //?Float?is?a?constraint?that?permits?any?floating-point?type.
          type?Float?interface?{?...?}

          //?Complex?is?a?constraint?that?permits?any?complex?numeric?type.
          type?Complex?interface?{?...?}

          //?Ordered?is?a?constraint?that?permits?any?ordered?type:?any?type?that?supports?the?operators?=?>.
          type?Ordered?interface?{?...?}

          //?Slice?is?a?constraint?that?matches?slices?of?any?element?type.
          type?Slice[Elem?any]?interface?{?~[]Elem?}

          //?Map?is?a?constraint?that?matches?maps?of?any?element?and?value?type.
          type?Map[Key?comparable,?Val?any]?interface?{?~map[Key]Val?}

          //?Chan?is?a?constraint?that?matches?channels?of?any?element?type.
          type?Chan[Elem?any]?interface?{?~chan?Elem?}

          然而簡(jiǎn)潔一直是 Go 追求的,于是有人提出,該包中的 Slice、Map、Chan 的約束根本不必要,包括簡(jiǎn)化約束字面值語(yǔ)法,具體討論見(jiàn):https://github.com/golang/go/issues/48424。于是,Go1.18 Beta 版本中,將這幾個(gè)約束類(lèi)型去掉了。

          然而,依然有人對(duì) constraints 有其他意見(jiàn),包括對(duì)包名不滿意。這個(gè) issue[1] 就提議將包名改為 of。

          當(dāng)然,最重要的是尚不清楚該包中哪些約束是重要的,應(yīng)該存在,哪些不應(yīng)該存在。

          當(dāng)初決定將 slices 和 maps 移到 x/exp 包,而留下 constraints,是因?yàn)?Go Team 認(rèn)為它是使用泛型的基礎(chǔ),但在實(shí)踐中似乎并沒(méi)有被證明是這樣。特別是,大多數(shù)代碼使用 any 或 comparable 即可,如果是這樣,也許 constraints 包很少被用到。

          當(dāng)然,其中的 constraints.Ordered 還是挺常用的,既然如此,也許應(yīng)該和 comparable 類(lèi)似,變成預(yù)聲明標(biāo)識(shí)符。

          經(jīng)過(guò) ?rsc 與 robpike、griesemer、ianlancetaylor 等討論,決定 Go1.18 中,將 constraints 和 slices、maps 一起,移到 x/exp 包中,之后在 Go 1.19 或 Go 1.20 中重新考慮它。

          雖然目前 Go1.18 Beta2 還包含 constraints 包,但已經(jīng)確定正式版本會(huì)移除。

          如果你需要使用 constraints.Ordered,建議先自己實(shí)現(xiàn),如下:

          type?Ordered?interface?{
          ?~int?|?~int8?|?~int16?|?~int32?|?~int64?|?~uint?|?~uint8?|?~uint16?|?~uint32?|?~uint64?|?~uintptr?|?~float32?|?~float64?|?~string
          }

          關(guān)于將 constraints 移到 x/exp 的詳細(xì)說(shuō)明,見(jiàn) rsc 提的 issue:https://github.com/golang/go/issues/50792。

          參考資料

          [1]

          issue: https://github.com/golang/go/issues/50348




          往期推薦


          我是 polarisxu,北大碩士畢業(yè),曾在 360 等知名互聯(lián)網(wǎng)公司工作,10多年技術(shù)研發(fā)與架構(gòu)經(jīng)驗(yàn)!2012 年接觸 Go 語(yǔ)言并創(chuàng)建了 Go 語(yǔ)言中文網(wǎng)!著有《Go語(yǔ)言編程之旅》、開(kāi)源圖書(shū)《Go語(yǔ)言標(biāo)準(zhǔn)庫(kù)》等。


          堅(jiān)持輸出技術(shù)(包括 Go、Rust 等技術(shù))、職場(chǎng)心得和創(chuàng)業(yè)感悟!歡迎關(guān)注「polarisxu」一起成長(zhǎng)!也歡迎加我微信好友交流:gopherstudio

          瀏覽 97
          點(diǎn)贊
          評(píng)論
          收藏
          分享

          手機(jī)掃一掃分享

          分享
          舉報(bào)
          評(píng)論
          圖片
          表情
          推薦
          點(diǎn)贊
          評(píng)論
          收藏
          分享

          手機(jī)掃一掃分享

          分享
          舉報(bào)
          <kbd id="afajh"><form id="afajh"></form></kbd>
          <strong id="afajh"><dl id="afajh"></dl></strong>
            <del id="afajh"><form id="afajh"></form></del>
                1. <th id="afajh"><progress id="afajh"></progress></th>
                  <b id="afajh"><abbr id="afajh"></abbr></b>
                  <th id="afajh"><progress id="afajh"></progress></th>
                  国产女人18毛片水真 | 色撸撸日一日 | MD-0127 艾秋 分享骚货老婆 强势调教处男晚辈 | 久久夜色精品国产噜噜亚洲AV高 | 人人摸人人看人人 |