问答详情
源自:5-3 Go语言-切片的更多操作方法

​无论如何都编译不通过,但是代码在本地是完全正确的。这个是怎么回事?

var numbers4 = [...]int{1, 2, 3, 4, 5, 6, 7, 8, 9, 10}

    slice5 := numbers4[4:6:8]

    length := 2

    capacity := 4

    fmt.Printf("%v, %v\n", length == len(slice5), capacity == cap(slice5))

    slice5 = slice5[:cap(slice5)]

    slice5 = append(slice5, 11, 12, 13) //{ 5, 6, 7, 8, 11, 12, 13}

    length = 7

    fmt.Printf("%v\n", length == len(slice5))

    slice6 := []int{0, 0, 0}

    copy(slice5, slice6) //{ 0, 0, 0, 8, 11, 12, 13}

    e2 := 0

    e3 := 8

    e4 := 11

    fmt.Printf("%v, %v, %v\n", e2 == slice5[2], e3 == slice5[3], e4 == slice5[4])


提问者:qq_子非鱼_2 2019-02-17 00:01

个回答

  • lyliuyuzhe
    2019-02-22 14:44:56

    package main

    import "fmt"


    开头加上这个