GO学习笔记07

作者: Q大疯zi | 来源:发表于2018-03-26 00:17 被阅读7次

    一、map

    1.定义

     package main
        
        import "fmt"
        
        func main() {
            //map只有len,没有cap
            var m1 map[int]string
            fmt.Println("m1 = ", m1)
            fmt.Println("len = ", len(m1))
            fmt.Println("-----------")
        
            //make方式创建
            m2 := make(map[int]string)
            fmt.Println("m2 = ", m2)
            fmt.Println("len = ", len(m2))
            fmt.Println("-----------")
        
            //指定长度2
            m3 := make(map[int]string, 2)
            m3[1] = "java"
            m3[2] = "go"
            m3[3] = "python"
            fmt.Println("m3 = ", m3)
            fmt.Println("len = ", len(m3))
        
        }
        
        
        m1 =  map[]
        len =  0
        -----------
        m2 =  map[]
        len =  0
        -----------
        m3 =  map[1:java 2:go 3:python]
        len =  3
    

    2.迭代

        package main
        
        import "fmt"
        
        func main() {
            m := map[int]string{1: "java", 2: "go", 3: "python"}
            for key, value := range m {
                fmt.Printf("key = %d, value = %s\n", key, value)
            }
        
            value, ok := m[0]
            if ok == true {
                fmt.Println("m[0] = ", value)
            } else {
                fmt.Println("不存在")
            }
            value1, ok1 := m[1]
            if ok1 == true {
                fmt.Println("m[0] = ", value1)
            } else {
                fmt.Println("不存在")
            }
        
        }
        
        key = 1, value = java
        key = 2, value = go
        key = 3, value = python
        不存在
        m[0] =  java
    

    3.删除

    m := map[int]string{1: "java", 2: "go", 3: "python"}
            for key, value := range m {
                fmt.Printf("key = %d, value = %s\n", key, value)
            }
        
            delete(m, 1)
            fmt.Println("m = ",m)
            
            key = 1, value = java
            key = 2, value = go
            key = 3, value = python
            m =  map[2:go 3:python]
    

    4.作为参数传递

        import "fmt"
        
        func main() {
            m := map[int]string{1: "java", 2: "go", 3: "python"}
            test(m) //引用传递
            fmt.Println("m = ", m)
        }
        func test(m map[int]string) {
            delete(m, 1)
        }
        
        m =  map[2:go 3:python]
    

    二、结构体

    1.定义

     package main
        
        import "fmt"
        
        type Student struct {
            id int
            name string
            age int
        }
        
        func main() {
            //顺序初始化每个成员必须初始化
            var s1 Student = Student{1,"tony",18}
            fmt.Println("s1 = ",s1)
            
            //指定初始化,未初始化的参数默认为类型的默认值
            s2:=Student{name:"Tom",age:19}
            fmt.Println("s2 = ",s2)
        }
        
        
        s1 =  {1 tony 18}
        s2 =  {0 Tom 19}
    

    2.指针类型的结构体

        package main
        
        import "fmt"
        
        type Student struct {
            id int
            name string
            age int
        }
        
        func main() {
            //顺序初始化每个成员必须初始化
            var p1 *Student = &Student{1,"tony",18}
            fmt.Println("p1 = ",p1)
        
            //指定初始化,未初始化的参数默认为类型的默认值
            p2:=&Student{name:"Tom",age:19}
            fmt.Println("p2 = ",p2)
        }
        
        p1 =  &{1 tony 18}
        p2 =  &{0 Tom 19}
    

    3.结构体普通成员操作

        package main
        
        import "fmt"
        
        type Student struct {
           id   int
           name string
           age  int
        }
        
        func main() {
           var s Student
           s.id = 1
           s.name = "tony"
           s.age = 19
        
           fmt.Println("s = ", s)
        }
        
        s =  {1 tony 19}
    

    4.结构体指针成员的操作

     package main
        
        import "fmt"
        
        type Student struct {
            id   int
            name string
            age  int
        }
        
        func main() {
            var s Student
            var p1 *Student
            p1 = &s
        
            //第一种
            p1.id = 1
            p1.name = "tony"
            p1.age = 19
            fmt.Println("p1 = ", p1)
            
            //第二种
            p2 := new(Student)
        
            p2.id = 2
            p2.age = 18
            p2.name = "tom"
            fmt.Println("p2 = ", p2)
        
        }
        
        p1 =  &{1 tony 19}
        p2 =  &{2 tom 18}
    

    5.结构体的比较和赋值

        结构体和数组一样可以赋值和比较
        代码略
    

    6.机构体作为函数参数01

        package main
        
        import "fmt"
        
        type Student struct {
            id   int
            name string
            age  int
        }
        
        func main() {
            s := Student{1, "tony", 18}
            test01(s)
            fmt.Println("main: ", s)
        
        }
        func test01(student Student) {
            student.id = 888
            fmt.Println("test01 = ", student)
        }
        
        
        test01 =  {888 tony 18}
        main:  {1 tony 18}
    

    7.结构体作为函数参数02

        package main
        
        import "fmt"
        
        type Student struct {
            id   int
            name string
            age  int
        }
        
        func main() {
            s := Student{1, "tony", 18}
            test01(&s)
            fmt.Println("main: ", s)
        
        }
        func test01(p *Student) {
            p.id = 888
            fmt.Println("test01 = ", p)
        }
        
        
        test01 =  &{888 tony 18}
        main:  {888 tony 18}
    

    8.可见性

     如果想使用别的包里面的函数、结构体类型、结构体成员
        函数名字、类型名、机构体成员变量名,首字母必须大写
    

    相关文章

      网友评论

        本文标题:GO学习笔记07

        本文链接:https://www.haomeiwen.com/subject/brhlcftx.html