设为首页 加入收藏

TOP

go 数组、切片
2018-11-22 12:08:21 】 浏览:130
Tags:数组 切片
  • 数组定义

 

    
    // 标准
    var a [5]int = [5]int{5, 4, 4, 3, 02}
    fmt.Println("a", a)

    // 自动推导类型
    b := [5]int{5, 4, 2, 3, 2}
    fmt.Println("b", b)

    // 其他默认为0
    c := [5]int{3, 5}
    fmt.Println("c", c)

    // 指定部分下标值
    d := [5]int{2: 10, 4: 9}
    fmt.Println("d", d)

    //a [5 4 4 3 2]
    //b [5 4 2 3 2]
    //c [3 5 0 0 0]
    //d [0 0 10 0 9]

 

  • 切片定义
  • func createSlice() {
        //方式1 自动推导类型
        s1 := []int{1, 2, 3, 4}
        fmt.Println(s1)
        //[1 2 3 4]
    
        //方式2  make函数  make(type,len,cap)
        s2 := make([]int, 5, 10)
        fmt.Println("s2=====", s2)
        fmt.Println("len(s)=", len(s2))
        fmt.Println("cap(s)=", cap(s2))
        //s2===== [0 0 0 0 0]
        //len(s)= 5
        //cap(s)= 10
    
        // 不写cap,则cap=len
        s3 := make([]int, 5)
        fmt.Println("s2=====", s3)
        fmt.Println("len(s)=", len(s3))
        fmt.Println("cap(s)=", cap(s3))
        //s2===== [0 0 0 0 0]
        //len(s)= 5
        //cap(s)= 5
    }

     

】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
上一篇Go基础系列:Go实现工作池的两种.. 下一篇golang 调用windows API 中文的处..

最新文章

热门文章

Hot 文章

Python

C 语言

C++基础

大数据基础

linux编程基础

C/C++面试题目