go語言調用append以後是否從新分配內存?

查看tidb源代碼:::util/charset/charset.go,下面有段代碼:app

// GetAllCharsets gets all charset descriptions in the local charsets.
func GetAllCharsets() []*Desc {
    descs := make([]*Desc, 0, len(charsets))
    // The charsetInfos is an array, so the iterate order will be stable.
    for _, ci := range charsetInfos {
        c, ok := charsets[ci.Name]
        if !ok {
            continue
        }
        desc := &Desc{
            Name:             c.Name,
            DefaultCollation: c.DefaultCollation.Name,
            Desc:             c.Desc,
            Maxlen:           c.Maxlen,
        }
        descs = append(descs, desc)
    }
    return descs
}

查看append,思考一個問題,調用本方法以後,descs內存地址是否從新分配?函數

特作以下例子進行測試:性能

type Names struct {
    Name string
}
mynames := make([]*Names, 0)
    for i := 0; i < 10; i++ {
        my := &Names{
            Name: "append"}
        mynames = append(mynames, my)
        fmt.Println(&mynames)
    }

運行結果以下:測試

image

從10次的輸出結果來看,調用append內部函數,不改變內存地址,也就是不產生性能損耗。spa

相關文章
相關標籤/搜索