Golang Array Slice区别

Array赋值是直接拷贝
Slice赋值是类似引用
package main

import (
    "fmt"
    "reflect"
)

func main() {
    fmt.Println("running...")
        // Array
    var arr = [3]string{"1", "2", ""}
    fmt.Println(arr, reflect.TypeOf(arr))
        // Slice
    var arr1 = []string{"1", "2", ""}
    fmt.Println(arr1, reflect.TypeOf(arr1))

    arr_ := arr
    arr_[2] = "3"
    fmt.Println(arr, arr_)

    arr1_ := arr1
    arr1_[2] = "3"
    fmt.Println(arr1, arr1_)

    fmt.Scanln()
    fmt.Println("end")
}


发表于:2016-11-19 13:08:04

原文链接(转载请保留): http://www.multisilicon.com/blog/a36291742.html

友情链接: MICROIC
首页