美文网首页Go
Go array & slice

Go array & slice

作者: JaedenKil | 来源:发表于2019-03-06 10:43 被阅读0次

    A slice does not store any data, it just describes a section of an underlying array.

    Changing the elements of a slice modifies the corresponding elements of its underlying array.

    Other slices that share the same underlying array will see those changes.

    When initialising a variable with a composite literal, Go requires that each line of the composite literal end with a comma, even the last line of your declaration.

    package main
    
    import "fmt"
    
    func main() {
        names := [4]string{
            "Alice",
            "Jack",
            "Johnny",
            "Zed",
        }
        fmt.Println(names)
    
        a := names[0: 2]
        b := names[1: 3]
        fmt.Println(a, b)
    
        b[0] = "XXX"
        fmt.Println(a, b)
        fmt.Println(names)
    }
    
    [Alice Jack Johnny Zed]
    [Alice Jack] [Jack Johnny]
    [Alice XXX] [XXX Johnny]
    [Alice XXX Johnny Zed]
    

    相关文章

      网友评论

        本文标题:Go array & slice

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