Go foundation

variable definition

basic method:

var variableName typevar person string

multiple variable definition:

var vname1, vname2, vname3 type

define the variable and initialize it:

var variableName type = value

initialize multiple variables:

var vname1, vname2, vname3 type = v1, v2, v3

or simplify it:

var vname1, vname2, vname3 = v1, v2, v3

or do the most simplified one:

vname1, vname2, vname3 := v1, v2, v3

Noted that this version should only be adopted inside the functions, or the complation will fail.

So the definition withe the prefix var can be used for the global variables.

_ is a special variable name, all values allocated to it will be deprecated

_, b := 34, 35

Declared but not used variables will trigger mistakes, which is not permmited in go.

Const

Const are those whose values have been determined when compiling.

const constantName = value//if necessary, allocate the type of the constconst Pi float32 = 3.1415926const prefix = "astaxie_"

Other types

Boolean, int, uint, int8, int16, int32, int64, byte, uint8, uint16, uint32, uint64, float32, float64

  • rune is equivalent to int32, byte is equivalent to uint8.

  • int's lenth is 32, but it is not equal to int32.

  • No float, the deflaut one is float64

  • complex is supported, complex128(64+64i) and complex64(32+32i)

var c complex64 = 5+5ifmt.Printf("Value is: %v",c)

String

Use ""or to define the String in between.

var frenchHello stringvar emptyString string = ""func test(){    no, yes, maybe := "no", "yes", "maybe"    japaneseHello := "Konichiwa"    frenchHello := "Bonjour"}

Noted that string is not changable

var s string = "hello"s[0] = 'c'//raise mistake "cannot assign to s[0]"

here is the method to do the modification

s := "hello"c := []byte(s) //convert s to []bytec[0] = 'c's2 := string(c) //convert it back

Or use slice to modify the string

s := "hello"h := "c" + s[1:]fmt.Printf("%s\n",h)

+ can connect two strings together

s := "hello"h := "world"a := s+hfmt.Printf("%s\n",a)

can preserve the initial format of the string, all things will remain the same.

m := `Hello        World`fmt.Printf("%s\n",m)

Here is the output:

Hello    World

Error

The type of error is specially for handling mistakes. There us a package of errors.

err := errors.New("emit macho dwarf: elf header corrupted")if err != nil{    fmt.Printf(err)}

Storage of data

[圖片上傳失敗...(image-c53635-1540311861796)]

Some skills

  • declaration by groups
import(    "fmt"    "os")const(    i = 100    pi = 3.1415    prefix = "Go_")var (    i int    pi float32    prefix string)
  • iota enumeration

One more lines of const, one more the value of iota

package mainimport (    "fmt")const (    x = iota //x = 0    y = iota //y = 1    z //z = 2, with omission declaration)const v = iota //one more keyword const, reset the value of iotaconst (    h,j,i = iota,iota,iota //all is 0, three should match three)const (    a = iota    //a = 0    b = "B"    c,d,e = iota  //c,d,e = 1)

Default rules

  • variables with capital letter are public, other packages could read the value, variables with lowercase letters are private.

  • Same as the fuction names.

array

Declaration

var arr [n]type

n is the length of the array, type is the elements this array stores,

var arr [10]intarr[0] = 42arr[1] = 13fmt.Println(arr[9]) //The initial value of a[9] is 0

Declaration inside the functions:

a := [3]int{1,2,3}b := [10]int{1,2,3}c := [...]int{4,5,6} //omit the length, count the number of elements instead

Matraix:

doubleArray := [2][4]int{    {1,2,3,4}    {5,6,7,8}}

Noted that when we pass one array into the function, we pass the copy of this array rather than its pointer.

if you wanted to modify the elements anyway, use slice.

Slice

Or could be described as "dynamical array", do not need the specification of length.

var fslice []int

Initialization:

slice := []byte {'a','b','c'}

slice could be picked from an existed array by array[i:j], array[i] is included, array[j] is excluded, the length is j-i

var ar = [10]int {1,2,3,4,5,6,7,8,9,10}var a, b []inta = ar[2,5]b = ar[3,5]

[圖片上傳失敗...(image-229275-1540311861796)]

When changing the values in the slice, the corresponding value in the array will be changed,too.

The interfaces in slice:

[圖片上傳失敗...(image-b74cc1-1540311861796)]

  • len can get the length

  • cap can get the capacity

  • append can add one or more elements and return the slice

capacity could be specified now:

slice = array[2:4:7] //capacity is 7-2

map

The dictionary in Python, whose index could be many types besides int.

Noted that the declaration should be outside the function, while the memory allocation should be inside the function.

var numbers map[string]int //declaration, should initialize it by make afterwardsnumbers = make(map[string]int)numbers["one"] = 1numbers["two"] = 2
  • map is unordered, which should be acquired by key rather than index

  • map has two return, the first one is the value, the second one is the boolean

rating := map[string]float32{"C":5,"Go":4.5,"Python":4.5}csharpRating, ok := rating["C#"]if ok{    fmt.Println("C# is in the map and its rating is ", cssharpRating)}delete(rating, "C#")

Differences between make and new

  • make: allocate memory for map,slice,channel,return T type rather than *T.

  • new: allocate memory for various types, the default value is 0, return *T(its address) pointingto T's value

Zero values

int     0int8    0int32   0int64   0uint    0x0rune    0 //rune的實(shí)際類型是 int32byte    0x0 // byte的實(shí)際類型是 uint8float32 0 //長(zhǎng)度為 4 bytefloat64 0 //長(zhǎng)度為 8 bytebool    falsestring  ""

The default padding values when you new the type

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
  • 序言:七十年代末,一起剝皮案震驚了整個(gè)濱河市七问,隨后出現(xiàn)的幾起案子蜓耻,更是在濱河造成了極大的恐慌,老刑警劉巖械巡,帶你破解...
    沈念sama閱讀 217,734評(píng)論 6 505
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件刹淌,死亡現(xiàn)場(chǎng)離奇詭異,居然都是意外死亡讥耗,警方通過(guò)查閱死者的電腦和手機(jī)有勾,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 92,931評(píng)論 3 394
  • 文/潘曉璐 我一進(jìn)店門,熙熙樓的掌柜王于貴愁眉苦臉地迎上來(lái)古程,“玉大人柠衅,你說(shuō)我怎么就攤上這事〖眨” “怎么了菲宴?”我有些...
    開(kāi)封第一講書人閱讀 164,133評(píng)論 0 354
  • 文/不壞的土叔 我叫張陵贷祈,是天一觀的道長(zhǎng)。 經(jīng)常有香客問(wèn)我喝峦,道長(zhǎng)势誊,這世上最難降的妖魔是什么? 我笑而不...
    開(kāi)封第一講書人閱讀 58,532評(píng)論 1 293
  • 正文 為了忘掉前任谣蠢,我火速辦了婚禮粟耻,結(jié)果婚禮上,老公的妹妹穿的比我還像新娘眉踱。我一直安慰自己挤忙,他們只是感情好,可當(dāng)我...
    茶點(diǎn)故事閱讀 67,585評(píng)論 6 392
  • 文/花漫 我一把揭開(kāi)白布谈喳。 她就那樣靜靜地躺著册烈,像睡著了一般。 火紅的嫁衣襯著肌膚如雪婿禽。 梳的紋絲不亂的頭發(fā)上赏僧,一...
    開(kāi)封第一講書人閱讀 51,462評(píng)論 1 302
  • 那天,我揣著相機(jī)與錄音扭倾,去河邊找鬼淀零。 笑死,一個(gè)胖子當(dāng)著我的面吹牛膛壹,可吹牛的內(nèi)容都是我干的驾中。 我是一名探鬼主播,決...
    沈念sama閱讀 40,262評(píng)論 3 418
  • 文/蒼蘭香墨 我猛地睜開(kāi)眼模聋,長(zhǎng)吁一口氣:“原來(lái)是場(chǎng)噩夢(mèng)啊……” “哼肩民!你這毒婦竟也來(lái)了?” 一聲冷哼從身側(cè)響起撬槽,我...
    開(kāi)封第一講書人閱讀 39,153評(píng)論 0 276
  • 序言:老撾萬(wàn)榮一對(duì)情侶失蹤,失蹤者是張志新(化名)和其女友劉穎趾撵,沒(méi)想到半個(gè)月后侄柔,有當(dāng)?shù)厝嗽跇?shù)林里發(fā)現(xiàn)了一具尸體,經(jīng)...
    沈念sama閱讀 45,587評(píng)論 1 314
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡占调,尸身上長(zhǎng)有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 37,792評(píng)論 3 336
  • 正文 我和宋清朗相戀三年暂题,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片究珊。...
    茶點(diǎn)故事閱讀 39,919評(píng)論 1 348
  • 序言:一個(gè)原本活蹦亂跳的男人離奇死亡薪者,死狀恐怖,靈堂內(nèi)的尸體忽然破棺而出剿涮,到底是詐尸還是另有隱情言津,我是刑警寧澤攻人,帶...
    沈念sama閱讀 35,635評(píng)論 5 345
  • 正文 年R本政府宣布,位于F島的核電站悬槽,受9級(jí)特大地震影響怀吻,放射性物質(zhì)發(fā)生泄漏。R本人自食惡果不足惜初婆,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 41,237評(píng)論 3 329
  • 文/蒙蒙 一蓬坡、第九天 我趴在偏房一處隱蔽的房頂上張望。 院中可真熱鬧磅叛,春花似錦屑咳、人聲如沸。這莊子的主人今日做“春日...
    開(kāi)封第一講書人閱讀 31,855評(píng)論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽(yáng)。三九已至访雪,卻和暖如春详瑞,著一層夾襖步出監(jiān)牢的瞬間,已是汗流浹背臣缀。 一陣腳步聲響...
    開(kāi)封第一講書人閱讀 32,983評(píng)論 1 269
  • 我被黑心中介騙來(lái)泰國(guó)打工坝橡, 沒(méi)想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留,地道東北人精置。 一個(gè)月前我還...
    沈念sama閱讀 48,048評(píng)論 3 370
  • 正文 我出身青樓计寇,卻偏偏與公主長(zhǎng)得像,于是被迫代替她去往敵國(guó)和親脂倦。 傳聞我的和親對(duì)象是個(gè)殘疾皇子番宁,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 44,864評(píng)論 2 354

推薦閱讀更多精彩內(nèi)容

  • rljs by sennchi Timeline of History Part One The Cognitiv...
    sennchi閱讀 7,331評(píng)論 0 10
  • **2014真題Directions:Read the following text. Choose the be...
    又是夜半驚坐起閱讀 9,497評(píng)論 0 23
  • 筆墨里的時(shí)光#9/30 讀書《你的孤獨(dú)雖敗猶榮》 今天是魚爸的生日,結(jié)婚已經(jīng)12年了赖阻,給魚爸過(guò)生日的次數(shù)少之又少蝶押。...
    檸檬草LF閱讀 195評(píng)論 7 5
  • 剛落地哪知天降一塊巨石,正好砸中了清風(fēng)的雙腿火欧,劇烈的疼痛差點(diǎn)使他暈過(guò)去棋电,但他知道機(jī)緣便在眼前,不能錯(cuò)過(guò)……強(qiáng)忍著疼...
    謫仙韓大人閱讀 342評(píng)論 0 1
  • 這個(gè)月內(nèi)學(xué)習(xí)了諸多教學(xué)上的東西苇侵,希望借助專家的指導(dǎo)快速提升自己的教學(xué)水準(zhǔn)赶盔。專家很多,各自成體系榆浓,有理論有...
    一顆麥稻閱讀 605評(píng)論 0 0