變量聲明簡(jiǎn)寫:=???
在函數(shù)內(nèi)部,:=使用隱式類型,可以用來(lái)替換var聲明
在函數(shù)外部,每一個(gè)語(yǔ)句必須以一個(gè)關(guān)鍵字開(kāi)頭,比如var func等,因此:=不可用在函數(shù)外部
Unlike regular variable declarations, a short variable declaration may redeclare variables provided they were originally declared earlier in the same block (or the parameter lists if the block is the function body) with the same type, and at least one of the non-blank variables is new. As a consequence, redeclaration can only appear in a multi-variable short declaration. Redeclaration does not introduce a new variable; it just assigns a new value to the original.
redeclaration 相當(dāng)于給原來(lái)的變量賦值,并未聲明新的變量,因此在函數(shù)外使用是不允許的。
還有一個(gè)原因,可能是保證語(yǔ)法的一致性,函數(shù)外都會(huì)以一個(gè)關(guān)鍵字來(lái)開(kāi)頭。
go 的類型推導(dǎo)(Type inference)
i := 42 // int
f := 3.142 // float64
g := 0.867 + 0.5i // complex128
或者(等價(jià))
var i = 42 // int
var f = 3.142 // float64
var g = 0.867 + 0.5i // complex128
編譯器在處理的時(shí)候會(huì)進(jìn)行類型推導(dǎo),從而決定最終值的類型。

類型轉(zhuǎn)換(Type conversions)
跟C語(yǔ)言不同的是(C語(yǔ)言會(huì)在賦值時(shí)進(jìn)行隱式轉(zhuǎn)換),go 在兩種不同的類型的項(xiàng)中,需要顯示的進(jìn)行類型轉(zhuǎn)換。這里要與類型推導(dǎo)區(qū)分開(kāi)來(lái)。
類型推導(dǎo)是在不指明類型時(shí)根據(jù)值推導(dǎo)
指定變量的類型但類型不匹配必須要顯式類型轉(zhuǎn)換

常量聲明 const
常量可以是字符、字符串、布爾型、數(shù)值,不可以用:=來(lái)聲明常量
