go语言学习,一——变量的声明和定义

最近比较闲 & 颓废了几天之后该学新的东西了。

&对服务器比较感兴趣,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中的变量的声明和定义完成