<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>

          為什么 Go 中的 Receiver Name 不推薦使用 this 或者 self

          共 1926字,需瀏覽 4分鐘

           ·

          2020-08-28 18:30

          前言

          在日常的開發(fā)中我們除了定義函數(shù)以外, 我們還會定義一些方法。這本來沒有什么, 但是一些從 PHP 或者其他面向?qū)ο笳Z言轉(zhuǎn) Go 的同學(xué)往往會把 receiver name 命名為 this, self, me 等。

          筆者在實(shí)際項(xiàng)目開發(fā)中也遇到類似的同學(xué), 屢次提醒卻沒有效果,于是決心寫下這篇文章以便好好說服這些同學(xué)。

          CR 標(biāo)準(zhǔn)做法

          首先我們來看一下 GO 推薦的標(biāo)準(zhǔn)命名Receiver Names,以下內(nèi)容摘抄自 https://github.com/golang/go/wiki/CodeReviewComments#receiver-names:

          The name of a method's receiver should be a reflection of its identity; often a one or two letter abbreviation of its type suffices (such as "c" or "cl" for "Client"). Don't use generic names such as "me", "this" or "self", identifiers typical of object-oriented languages that gives the method a special meaning. In Go, the receiver of a method is just another parameter and therefore, should be named accordingly. ...

          簡單翻譯總結(jié)有如下 2 點(diǎn):

          1. 方法接受者名稱應(yīng)反映其身份, 并且不要使用me, this, self這些面向?qū)ο笳Z言的典型標(biāo)志符。
          2. 在 go 中方法接受者其實(shí)就是方法的另一個參數(shù)。

          Receiver 是方法的第一個參數(shù)!

          上面的第二點(diǎn), 可能不是很好理解,所以我們直接看下面的 demo:

          //?T?...
          type?T?int

          //?Println?...
          func?(t?T)?Println()?{
          ?fmt.Println("value:?%v",?t)
          }

          func?main()?{
          ?t?:=?T(1)
          ?t.Println()
          ?T.Println(t)
          }
          //?output:
          value:?1
          value:?1

          通過上面的 demo, 我們知道接受者可以直接作為第一個參數(shù)傳遞給方法的。而t.Println()應(yīng)該就是 Go 中的一種語法糖了。

          到這里可能有同學(xué)又要問了, 既然 Go 提供了這種語糖,那我們這樣命名有什么問題呢?筆者先不著急解釋, 我們繼續(xù)看下面的 demo:

          //?Test?...
          type?Test?struct?{
          ?A?int
          }

          //?SetA?...
          func?(t?Test)?SetA(a?int)?{
          ?t.A?=?a
          }

          //?SetA1?...
          func?(t?*Test)?SetA1(a?int)?{
          ?t.A?=?a
          }

          func?main()?{
          ?t?:=?Test{
          ??A:?3,
          ?}
          ?fmt.Println("demo1:")
          ?fmt.Println(t.A)
          ?t.SetA(5)
          ?fmt.Println(t.A)
          ?t1?:=?Test{
          ??A:?4,
          ?}
          ?fmt.Println("demo2:")
          ?fmt.Println(t1.A)
          ?(&t1).SetA1(6)
          ?fmt.Println(t1.A)
          }
          //?output:
          demo1:
          3
          3
          demo2:
          4
          6

          看上面的 demo 我們知道, 當(dāng) receiver 不是指針時(shí)調(diào)用 SetA 其值根本沒有改變。

          因?yàn)?Go 中都是值傳遞,所以你如果對 SetA 的 receiver 的名稱命名為this, self等,它就已經(jīng)失去了本身的意義——“調(diào)用一個對象的方法就是向該對象傳遞一條消息”。而且對象本身的屬性也并不一定會發(fā)生改變。

          綜上: 請各位讀者在對 receiver 命名時(shí)不要再用 this, self 等具有特殊含義的名稱啦。




          推薦閱讀



          學(xué)習(xí)交流 Go 語言,掃碼回復(fù)「進(jìn)群」即可


          站長 polarisxu

          自己的原創(chuàng)文章

          不限于 Go 技術(shù)

          職場和創(chuàng)業(yè)經(jīng)驗(yàn)


          Go語言中文網(wǎng)

          每天為你

          分享 Go 知識

          Go愛好者值得關(guān)注



          瀏覽 41
          點(diǎn)贊
          評論
          收藏
          分享

          手機(jī)掃一掃分享

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

          手機(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>
                  欧美成人电影一区 | 久久男人天堂 | 国产投拍导航 | 男人天堂社区 | 91偷拍与自偷拍精品无码 |