<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 快訊:新增的 Cut 函數(shù)太方便了

          共 2542字,需瀏覽 6分鐘

           ·

          2021-11-13 22:18

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

          大家好,我是 polarisxu。

          在編程中,字符串使用是最頻繁的。Go 語言對字符串相關(guān)的操作也提供了大量的 API,一方面,字符串可以向普通 slice 一樣進(jìn)行相關(guān)操作;另一方面,標(biāo)準(zhǔn)庫專門提供了一個包 strings 進(jìn)行字符串的操作。

          01 strings.Index 系列函數(shù)

          假如有一個這樣的需求:從 192.168.1.1:8080 中獲取 ip 和 port。

          我們一般會這么實現(xiàn):

          addr?:=?"192.168.1.1:8080"
          pos?:=?strings.Index(addr,?":")
          if?pos?==?-1?{
          ??panic("非法地址")
          }
          ip,?port?:=?addr[:pos],?addr[pos+1:]

          實際項目中,pos == -1 時應(yīng)該返回 error

          此處忽略通過 net.TCPAddr 方式得到,主要在于講解字符串處理。

          strings 包中,Index 相關(guān)函數(shù)有好幾個:

          func?Index(s,?substr?string)?int
          func?IndexAny(s,?chars?string)?int
          func?IndexByte(s?string,?c?byte)?int
          func?IndexFunc(s?string,?f?func(rune)?bool)?int
          func?IndexRune(s?string,?r?rune)?int
          func?LastIndex(s,?substr?string)?int
          func?LastIndexAny(s,?chars?string)?int
          func?LastIndexByte(s?string,?c?byte)?int
          func?LastIndexFunc(s?string,?f?func(rune)?bool)?int

          Go 官方統(tǒng)計了 Go 源碼中使用相關(guān)函數(shù)的代碼:

          • 311 Index calls outside examples and testdata.
          • 20 should have been Contains
          • 2 should have been 1 call to IndexAny
          • 2 should have been 1 call to ContainsAny
          • 1 should have been TrimPrefix
          • 1 should have been HasSuffix

          相關(guān)需求是這么多,而 Index 顯然不是處理類似需求最好的方式。于是 Russ Cox 提議,在 strings 包中新增一個函數(shù) Cut,專門處理類似的常見。

          02 新增的 Cut 函數(shù)

          Cut 函數(shù)的簽名如下:

          func?Cut(s,?sep?string)?(before,?after?string,?found?bool)

          將字符串 s 在第一個 sep 處切割為兩部分,分別存在 before 和 after 中。如果 s 中沒有 sep,返回 s,"",false

          根據(jù) Russ Cox 的統(tǒng)計,Go 源碼中 221 處使用 Cut 會更好。

          針對上文提到的需求,改用 Cut 函數(shù):

          addr?:=?"192.168.1.1:8080"
          ip,?port,?ok?:=?strings.Cut(addr,?":")

          是不是很清晰?!

          這是又一個改善生活質(zhì)量的優(yōu)化。

          針對該函數(shù),官方提供了如下示例:

          package?main

          import?(
          ?"fmt"
          ?"strings"
          )

          func?main()?{
          ?show?:=?func(s,?sep?string)?{
          ??before,?after,?found?:=?strings.Cut(s,?sep)
          ??fmt.Printf("Cut(%q,?%q)?=?%q,?%q,?%v\n",?s,?sep,?before,?after,?found)
          ?}
          ?show("Gopher",?"Go")
          ?show("Gopher",?"ph")
          ?show("Gopher",?"er")
          ?show("Gopher",?"Badger")
          }

          //?Output:
          /*
          Cut("Gopher",?"Go")?=?"",?"pher",?true
          Cut("Gopher",?"ph")?=?"Go",?"er",?true
          Cut("Gopher",?"er")?=?"Goph",?"",?true
          Cut("Gopher",?"Badger")?=?"Gopher",?"",?false
          */

          03 總結(jié)

          從 PHP 轉(zhuǎn)到 Go 的朋友,肯定覺得 Go 標(biāo)準(zhǔn)庫應(yīng)該提供更多便利函數(shù),讓生活質(zhì)量更好。在 Go 社區(qū)這么多年,也確實聽到了不少這方面的聲音。

          但 Go 官方不會輕易增加一個功能。就 Cut 函數(shù)來說,官方做了詳細(xì)調(diào)研、說明,具體可以參考這個 issue:bytes, strings: add Cut[1],可見 bytes 同樣的增加了 Cut 函數(shù)。

          有人提到,為什么不增加 LastCut?Russ Cox 的解釋是,LastIndex 的調(diào)用次數(shù)明顯少于 Index,因此暫不提供 LastCut。

          做一個決定,不是瞎拍腦袋的~

          參考資料

          [1]

          bytes, strings: add Cut: https://github.com/golang/go/issues/46336




          往期推薦


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


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


          瀏覽 39
          點贊
          評論
          收藏
          分享

          手機(jī)掃一掃分享

          分享
          舉報
          評論
          圖片
          表情
          推薦
          點贊
          評論
          收藏
          分享

          手機(jī)掃一掃分享

          分享
          舉報
          <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>
                  国产豆花视频永久在线观看 | 熟女视频一区二区 | 老熟妇久久久XXX预见频 | 97视频网站 | 五月天黄色电影播放 |