AskOverflow.Dev

AskOverflow.Dev Logo AskOverflow.Dev Logo

AskOverflow.Dev Navigation

  • 主页
  • 系统&网络
  • Ubuntu
  • Unix
  • DBA
  • Computer
  • Coding
  • LangChain

Mobile menu

Close
  • 主页
  • 系统&网络
    • 最新
    • 热门
    • 标签
  • Ubuntu
    • 最新
    • 热门
    • 标签
  • Unix
    • 最新
    • 标签
  • DBA
    • 最新
    • 标签
  • Computer
    • 最新
    • 标签
  • Coding
    • 最新
    • 标签
主页 / coding / 问题 / 76938226
Accepted
Mandroid
Mandroid
Asked: 2023-08-20 14:42:44 +0800 CST2023-08-20 14:42:44 +0800 CST 2023-08-20 14:42:44 +0800 CST

golang:切片之间的数组共享

  • 772

这解释了切片的附加功能。

正如它所说,追加返回更新后的切片。

那么这是否意味着新创建的切片不与现有切片共享底层数组?

对于其他切片操作,例如 mySlice[x:y],新切片将与 mySlice 共享底层数组,如下所示。

PS:测试代码:

var names = make([]string, 4, 10)
names1 := append(names, "Tom")

因此在这种情况下,名称中有足够的可用容量。因此追加不能创建新的底层数组。

这输出:

[   ]
[    Tom]

输出不应该与共享底层数组相同吗?

我肯定在这里遗漏了一些非常基本的东西。

go
  • 2 2 个回答
  • 44 Views

2 个回答

  • Voted
  1. Eugene Mikhalev
    2023-08-20T16:21:25+08:002023-08-20T16:21:25+08:00

    Append 返回新的切片结构,但底层数组可以共享也可以不共享,具体取决于容量:如果容量足够则共享,否则不共享。

    Slice 结构体具有长度、容量和指向底层数组字段的指针:

    type slice struct {
        array unsafe.Pointer
        len   int
        cap   int
    }
    

    以下是一些示例:

    package main
    
    import "fmt"
    
    func foo(a []int) {
        // new array allocated, returned new slice struct
        // and it is assigned to local copy of slice struct
        a = append(a, 7)
    
        // element changed in local copy of slice struct
        a[1] = 7
    }
    
    func bar(a *[]int) {
        // new array allocated, returned new slice struct
        // and it is assigned to the variable declared on first line of main function
        // as it is passed by reference
        *a = append(*a, 7)
    }
    
    func main() {
        // new slice, len and capacity equals number of elements
        a := []int{1, 2, 3, 4, 5, 6}
        fmt.Printf("a[1]=%d\n", a[1])
    
        // array shared, will print a[1]=10
        b := a[1:3]
        b[0] = 10
        fmt.Printf("1. a[1]=%d\n", a[1])
    
        // new array allocated, will print a[1]=10
        b = append(b, a...)
        b[0] = 100
        fmt.Printf("2. a[1]=%d\n", a[1])
    
        // new array allocated, will print a[1]=10
        foo(a)
        fmt.Printf("3. a[1]=%d\n", a[1])
    
        // will print [1 10 3 4 5 6 7]
        bar(&a)
        fmt.Printf("4. a=%v\n", a)
    }
    
    • 0
  2. Best Answer
    dev.bmax
    2023-08-20T16:23:12+08:002023-08-20T16:23:12+08:00

    你是对的,names1使用与 相同的底层数组names。

    不,输出不应该相同,因为names长度为 4,而 names1长度为 5。请注意,两者都有容量 (10)。

    这是一个例子,可能会稍微澄清这一点:

    func main() {
        emptyNames := make([]string, 0, 10)
        notEmptyNames := append(emptyNames, "Jerry")
        extendedNames := emptyNames[:1] // OK, because 1 < cap(emptyNames)
        fmt.Println("emptyNames:", emptyNames)
        //emptyNames: []
        fmt.Println("notEmptyNames:", notEmptyNames)
        //notEmptyNames: [Jerry]
        fmt.Println("extendedNames:", extendedNames)
        //extendedNames: [Jerry]
    }
    
    • 0

相关问题

  • golang testscript .txtar 语法,用于 stderr 或 stdout 中包含的文本

  • Golang:访问“any”类型泛型上的字段[重复]

Sidebar

Stats

  • 问题 205573
  • 回答 270741
  • 最佳答案 135370
  • 用户 68524
  • 热门
  • 回答
  • Marko Smith

    使用 <font color="#xxx"> 突出显示 html 中的代码

    • 2 个回答
  • Marko Smith

    为什么在传递 {} 时重载解析更喜欢 std::nullptr_t 而不是类?

    • 1 个回答
  • Marko Smith

    您可以使用花括号初始化列表作为(默认)模板参数吗?

    • 2 个回答
  • Marko Smith

    为什么列表推导式在内部创建一个函数?

    • 1 个回答
  • Marko Smith

    我正在尝试仅使用海龟随机和数学模块来制作吃豆人游戏

    • 1 个回答
  • Marko Smith

    java.lang.NoSuchMethodError: 'void org.openqa.selenium.remote.http.ClientConfig.<init>(java.net.URI, java.time.Duration, java.time.Duratio

    • 3 个回答
  • Marko Smith

    为什么 'char -> int' 是提升,而 'char -> Short' 是转换(但不是提升)?

    • 4 个回答
  • Marko Smith

    为什么库中不调用全局变量的构造函数?

    • 1 个回答
  • Marko Smith

    std::common_reference_with 在元组上的行为不一致。哪个是对的?

    • 1 个回答
  • Marko Smith

    C++17 中 std::byte 只能按位运算?

    • 1 个回答
  • Martin Hope
    fbrereto 为什么在传递 {} 时重载解析更喜欢 std::nullptr_t 而不是类? 2023-12-21 00:31:04 +0800 CST
  • Martin Hope
    比尔盖子 您可以使用花括号初始化列表作为(默认)模板参数吗? 2023-12-17 10:02:06 +0800 CST
  • Martin Hope
    Amir reza Riahi 为什么列表推导式在内部创建一个函数? 2023-11-16 20:53:19 +0800 CST
  • Martin Hope
    Michael A fmt 格式 %H:%M:%S 不带小数 2023-11-11 01:13:05 +0800 CST
  • Martin Hope
    God I Hate Python C++20 的 std::views::filter 未正确过滤视图 2023-08-27 18:40:35 +0800 CST
  • Martin Hope
    LiDa Cute 为什么 'char -> int' 是提升,而 'char -> Short' 是转换(但不是提升)? 2023-08-24 20:46:59 +0800 CST
  • Martin Hope
    jabaa 为什么库中不调用全局变量的构造函数? 2023-08-18 07:15:20 +0800 CST
  • Martin Hope
    Panagiotis Syskakis std::common_reference_with 在元组上的行为不一致。哪个是对的? 2023-08-17 21:24:06 +0800 CST
  • Martin Hope
    Alex Guteniev 为什么编译器在这里错过矢量化? 2023-08-17 18:58:07 +0800 CST
  • Martin Hope
    wimalopaan C++17 中 std::byte 只能按位运算? 2023-08-17 17:13:58 +0800 CST

热门标签

python javascript c++ c# java typescript sql reactjs html

Explore

  • 主页
  • 问题
    • 最新
    • 热门
  • 标签
  • 帮助

Footer

AskOverflow.Dev

关于我们

  • 关于我们
  • 联系我们

Legal Stuff

  • Privacy Policy

Language

  • Pt
  • Server
  • Unix

© 2023 AskOverflow.DEV All Rights Reserve