日韩无码专区无码一级三级片|91人人爱网站中日韩无码电影|厨房大战丰满熟妇|AV高清无码在线免费观看|另类AV日韩少妇熟女|中文日本大黄一级黄色片|色情在线视频免费|亚洲成人特黄a片|黄片wwwav色图欧美|欧亚乱色一区二区三区

RELATEED CONSULTING
相關(guān)咨詢(xún)
選擇下列產(chǎn)品馬上在線(xiàn)溝通
服務(wù)時(shí)間:8:30-17:00
你可能遇到了下面的問(wèn)題
關(guān)閉右側(cè)工具欄

新聞中心

這里有您想知道的互聯(lián)網(wǎng)營(yíng)銷(xiāo)解決方案
IntMake居然不是關(guān)鍵字?

本文轉(zhuǎn)載自微信公眾號(hào)「董澤潤(rùn)的技術(shù)筆記」,作者董澤潤(rùn)。轉(zhuǎn)載本文請(qǐng)聯(lián)系董澤潤(rùn)的技術(shù)筆記公眾號(hào)。

做網(wǎng)站、網(wǎng)站建設(shè)中從網(wǎng)站色彩、結(jié)構(gòu)布局、欄目設(shè)置、關(guān)鍵詞群組等細(xì)微處著手,突出企業(yè)的產(chǎn)品/服務(wù)/品牌,幫助企業(yè)鎖定精準(zhǔn)用戶(hù),提高在線(xiàn)咨詢(xún)和轉(zhuǎn)化,使成都網(wǎng)站營(yíng)銷(xiāo)成為有效果、有回報(bào)的無(wú)錫營(yíng)銷(xiāo)推廣。創(chuàng)新互聯(lián)建站專(zhuān)業(yè)成都網(wǎng)站建設(shè)十多年了,客戶(hù)滿(mǎn)意度97.8%,歡迎成都創(chuàng)新互聯(lián)客戶(hù)聯(lián)系。

這是一個(gè)小白問(wèn)題,有多少人知道 int 不是關(guān)鍵字?make 也不是關(guān)鍵字?

我們知道每種語(yǔ)言都有關(guān)鍵字和保留字的,而 go 以關(guān)鍵字少著稱(chēng),只有25個(gè)

 
 
 
  1. break        default      func         interface    select 
  2. case         defer        go           map          struct 
  3. chan         else         goto         package      switch 
  4. const        fallthrough  if           range        type 
  5. continue     for          import       return       var 

也就是說(shuō),我們常用的 make, cap, len不是關(guān)鍵字,就連基本數(shù)據(jù)類(lèi)型 int, int64, float 也都不是。但是 C 語(yǔ)言中關(guān)鍵字可是非常多的

make 內(nèi)置函數(shù)

 
 
 
  1. package main 
  2.  
  3. import "fmt" 
  4.  
  5. func main(){ 
  6.     make := func() string { 
  7.         return "hijacked" 
  8.     } 
  9.  
  10.     int := make()    // Completely OK, variable 'int' will be a string 
  11.     fmt.Println(int) // Prints "hijacked" 

這段代碼 make 變量是一個(gè)閉包,返回一個(gè)字符串,而 int 變量類(lèi)型是字符串。最后函數(shù)打印 hijacked. 顯然這段代碼很神經(jīng)病,誰(shuí)要這么寫(xiě)會(huì)被打死,但確是可以編譯成功的

同時(shí)如果想繼續(xù)用 make 創(chuàng)建 map, 或是用 int 聲明變量就會(huì)報(bào)錯(cuò)。本質(zhì)上 make, cap, len 都是 go 源碼中的函數(shù)名,有點(diǎn)泛型的意思

 
 
 
  1. // The make built-in function allocates and initializes an object of type 
  2. // slice, map, or chan (only). Like new, the first argument is a type, not a 
  3. // value. Unlike new, make's return type is the same as the type of its 
  4. // argument, not a pointer to it. The specification of the result depends on 
  5. // the type: 
  6. // Slice: The size specifies the length. The capacity of the slice is 
  7. // equal to its length. A second integer argument may be provided to 
  8. // specify a different capacity; it must be no smaller than the 
  9. // length. For example, make([]int, 0, 10) allocates an underlying array 
  10. // of size 10 and returns a slice of length 0 and capacity 10 that is 
  11. // backed by this underlying array. 
  12. // Map: An empty map is allocated with enough space to hold the 
  13. // specified number of elements. The size may be omitted, in which case 
  14. // a small starting size is allocated. 
  15. // Channel: The channel's buffer is initialized with the specified 
  16. // buffer capacity. If zero, or the size is omitted, the channel is 
  17. // unbuffered. 
  18. func make(t Type, size ...IntegerType) Type 
 
 
 
  1. func len(v Type) int 
 
 
 
  1. func cap(v Type) int 

上面是 runtime 中對(duì) make, len, cap 的函數(shù)定義,大家可以看注釋或是看 builtin.go. make 接收三種類(lèi)型參數(shù):Map, Channel, Slice. 返回值是類(lèi)型 T, 而不像 new 返回的是指針 *T

也就是說(shuō),變量名用 make, 只是在 main 函數(shù)這個(gè)詞法塊中普通的局部變量而己,同時(shí)遮蔽了 runtime 的 make 函數(shù)名

Predeclared identifiers

前面說(shuō)的是 make, 那么對(duì)于 int 呢?其實(shí)道理也一樣,這些都是 go 預(yù)定義的標(biāo)識(shí)符 Predeclared identifiers

 
 
 
  1. Types: 
  2.  bool byte complex64 complex128 error float32 float64 
  3.  int int8 int16 int32 int64 rune string 
  4.  uint uint8 uint16 uint32 uint64 uintptr 
  5.  
  6. Constants: 
  7.  true false iota 
  8.  
  9. Zero value: 
  10.  nil 
  11.  
  12. Functions: 
  13.  append cap close complex copy delete imag len 
  14.  make new panic print println real recover 

其實(shí)這些都 document 在 builtin.go,包括常見(jiàn)的整數(shù)類(lèi)型,true, false, iota, nil 以及常用的函數(shù) make, new, copy 等等,這些在其它語(yǔ)言可能都對(duì)應(yīng)著關(guān)鍵詞 keywords 或是保留詞

從編譯原理的角度看,identifiers 和 keywords 關(guān)鍵詞沒(méi)有本質(zhì)的區(qū)別,都是一個(gè)一個(gè) token 而己

官方告訴我們,這些預(yù)定義的標(biāo)識(shí)符在 universe block 塊中都是隱式定義的,所以我們才能直接用。那么什么是 universe block 呢?

 
 
 
  1. Block = "{" StatementList "}" . 
  2. StatementList = { Statement ";" } . 

除了上面這種顯示的語(yǔ)句塊,還有很多隱式的語(yǔ)句塊。大家要小心,因?yàn)楹芏鄷r(shí)候 variable shadow 就是因?yàn)檫@個(gè)隱式的

  • The universe block encompasses all Go source text. 通用塊包括 go 源碼文本
  • Each package has a package block containing all Go source text for that package. 每個(gè)包都有一個(gè)塊,包含該包的所有 Go 源代碼
  • Each file has a file block containing all Go source text in that file. 每個(gè)文件都有一個(gè)文件塊,包含該文件中的所有 Go 源碼
  • Each "if", "for", and "switch" statement is considered to be in its own implicit block. 每個(gè) if、for 和 switch 語(yǔ)句都被認(rèn)為是在自己的隱式塊中
  • Each clause in a "switch" or "select" statement acts as an implicit block. switch 或 select 語(yǔ)句中的每個(gè)子句都是一個(gè)隱式塊

我們就犯過(guò)錯(cuò)誤,命中了最后一條導(dǎo)致了變量 shadow. 那么問(wèn)題來(lái)了,為什么 go 選擇預(yù)定義標(biāo)識(shí)符的方式,而不是直接定義成 keywords 呢?Go prefers the universal block over keywords because declarations can be added to the universal block without breaking existing programs


分享文章:IntMake居然不是關(guān)鍵字?
鏈接地址:http://www.5511xx.com/article/coggppo.html