最近比較閑 & 頹廢了幾天之后該學新的東西了。
&對服務器比較感興趣,so在公司前輩的建議下學go語言
下面是我整理過的,學習過程中寫的練習代碼,希望能對正在學習go語言的同學們有些幫助,&自己備忘一下。
大家一起學習一起進步!!!
// GoStudy1 project main.go /*go語言學習 變量的聲明和初始化 */ package main import ( //這里是小括號 "fmt" ) func main() { //變量的定義 var v21 int32 var v22 int = 2 var v23 = 3 //被自動識別為int類型 v24 := 4 //簡易聲明&定義的方式 v21 = int32(v23) fmt.Println("v21 is", v21) //變量只聲明&初始化而不使用編譯不能通過 fmt.Println("v22 is", v22) fmt.Println("v23 is", v23) fmt.Println("v24 is", v24) fmt.Println("***********************") //變量賦值 v22, v23 = v23, v22 //可以直接交換 fmt.Println("v22 is", v22) fmt.Println("v23 is", v23) fmt.Println("***********************") v22, v23, v24 = v24, v24, v24 //可以多個一起賦值 fmt.Println("v22 is", v22) fmt.Println("v23 is", v23) fmt.Println("v24 is", v24) fmt.Println("***********************") //下划線表示匿名變量,省去了不必要的變量定義 _, v24 = test() fmt.Println("v24 is", v24) fmt.Println("***********************") //其他常用類型的變量 var ( //這里是小括號不是大括號 v31 float32 v32 [5]bool //bool默認值為false,不支持類型轉換 v33 []bool v34 *bool v35 struct { name string age int } v36, v37 map[string]bool v38 [2][2]int ) v31 = 5.3 v32 = [5]bool{true, false, false, true, false} v32[1] = true //v33 = []bool{true, false, true, false, true, false} //v33 = v32[:2] //v32中前兩個元素創建切片 //v33 = v32[2:] //從index 2開始以及之后的元素創建切片 //v33 = v32[:] //v32所有元素創建切片 v33 = v32[2:4] //index 2 到第四個(即index3);也可以基於切片來創建,只要不超出他的存儲能力都是合法的 //v33 = make([]bool, 3) //創建元素個數為3的切片 //v33 = make([]bool, 3, 6) //創建初始元素格式為3,最大元素個數為6的切片,這樣做是為了減少元素分配空間的開銷 //v33 = append(v33, true, false, false, true) //切片拓展 v332 := []bool{false, true} v33 = append(v33, v332...) //可以連接切片,后面...表示將切片打散 v331 := make([]bool, 2) copy(v331, v33) //將v33復制到v331,由於v331長度為3,所以只復制前三個到v331 v34 = &v32[0] v35.name = "Tom" v35.age = 18 v36 = make(map[string]bool) //初始化map v36["r1"] = false //添加元素 v36["r2"] = true delete(v36, "r2") delete(v36, "lol") //字典中沒有這個元素,不會報錯 v361, ok := v36["r1"] //字典中沒有該元素ok為false v37 = make(map[string]bool, 100) //可以初始定義存儲能力 v37 = map[string]bool{"h1": false, "h2": true} //右大括號不能換行 v38 = [2][2]int{{1, 2}, {3, 4}} fmt.Println("v31 is", v31) fmt.Println("v32 is", v32) fmt.Println("v33 is", v33, "space", cap(v33), "lenth", len(v33)) //分配的空間大小cap fmt.Println("v331 is", v331) fmt.Println("v34 is", v34) fmt.Println("v35 is", v35) fmt.Println("v36 is", v36, "lenth is", len(v36)) fmt.Println("v361 is", v361) fmt.Println("v361's ok is", ok) fmt.Println("v37 is", v37) fmt.Println("v38 is", v38) fmt.Println("***********************") //常量 const v41 int = 41 //將變量中的var替換成const就是在定義常量 const v42 float32 = 15.6 * 16.8 //右側可以(只能)是編譯期能夠運行簡單計算 const ( v43 = iota //iota沒出現一次const就重置為0 v44 = iota v45 = iota ) const v46 float32 = iota const ( v47 = 1 << iota v48 //此處省略,表示表達式和v47的一樣 v49 ) const ( v410 = iota v411 = 10 v412 //此處省略,表示表達式和v411的一樣 ) const ( //“枚舉”類型 AA = iota //首字母大寫表示包外可見,類似於public BB CC DD ) fmt.Println("v41 is", v41) fmt.Println("v42 is", v42) fmt.Println("v43 is", v43) fmt.Println("v44 is", v44) fmt.Println("v45 is", v45) fmt.Println("v46 is", v46) fmt.Println("v47 is", v47) fmt.Println("v48 is", v48) fmt.Println("v49 is", v49) fmt.Println("v410 is", v410) fmt.Println("v411 is", v411) fmt.Println("v412 is", v412) fmt.Println("***********************") //位運算 v51 := 1 v52 := 2 v53 := v51 << 2 v54 := v51 ^ v52 //異或 v55 := v51 & v53 v56 := v51 | v53 v57 := ^v51 //取反 fmt.Println("v51 is", v51) fmt.Println("v52 is", v52) fmt.Println("v53 is", v53) fmt.Println("v54 is", v54) fmt.Println("v55 is", v55) fmt.Println("v56 is", v56) fmt.Println("v57 is", v57) fmt.Println("***********************") //自動識別類型的注意事項 //3被自動識別成int v61 := 0.32 //自動的識別是float64 var v62 float32 = float32(v61) fmt.Println("v61 is", v61) fmt.Println("v62 is", v62) fmt.Println("***********************") //復數 var v71 complex64 = complex(12, 13.5) v72 := 15 + 0.1i fmt.Println("v71 is", v71, "real is", real(v71)) //實部 fmt.Println("v72 is", v72, "imag is", imag(v72)) //虛部 fmt.Println("***********************") //遍歷,數組,切片通用 v81 := [10]int{0, 1, 2, 3, 4, 5, 6, 7, 8, 9} lenth := len(v81) for i := 0; i < lenth; i++ { fmt.Println("array:", i, v81[i]) } for i, v := range v81 { //也可以用於字典,相當於foreach fmt.Println(i, v) } for _, v := range v81 { fmt.Println(v) } } func test() (v1 int, v2 int) { return 0, 100 }
以上,go中的變量的聲明和定義完成