Function 具有slice参数的函数将不会修改原始容量

Function 具有slice参数的函数将不会修改原始容量,function,go,append,return-value,slice,Function,Go,Append,Return Value,Slice,为什么在go中编辑函数内的切片不应用长度更新 快速响应:因为切片只是对原始数组的引用 正如您在下面的示例中所看到的,我有一个切片,它将通过外部函数在值中进行修改,但是在您从函数返回值并将其重新分配给原始切片之前,追加操作将被完全忽略 为了改进示例,我添加了传递引用的函数,并进行修改操作,以提高解决方案的舒适性 运行此操作将产生: START STATUS_____ slice [0 0 0 0 0 0 0 0] cap: 8 len: 8 inside the edit Sli

为什么在go中编辑函数内的切片不应用长度更新

快速响应:因为切片只是对原始数组的引用

正如您在下面的示例中所看到的,我有一个切片,它将通过外部函数在值中进行修改,但是在您从函数返回值并将其重新分配给原始切片之前,追加操作将被完全忽略

为了改进示例,我添加了传递引用的函数,并进行修改操作,以提高解决方案的舒适性

运行此操作将产生:

START STATUS_____
slice [0 0 0 0 0 0 0 0]
    cap: 8
    len: 8

inside the edit Slice function the sli is = [0 0 0 0 0 0 0 11 33 34 35]
and the direct assignment of the last element (7°) ar right working.

However, the slice append will not be applied to the original slice because this is an internal copy
EDIT RESULT STATUS_____
    slice [0 0 0 0 0 0 0 11]
    cap: 8
    len: 8

inside the update Slice function the sli is =  [0 0 0 0 0 0 0 11 12 13 14]
and this append will be applied to the original one becouse of the return value of this function is a         new slice
UPDATE RESULT STATUS_____
    slice [0 0 0 0 0 0 0 11 12 13 14]
    cap: 16
    len: 11
DIRECT APPEND RESULT STATUS_____
    slice [0 0 0 0 0 0 0 11 12 13 14 15 16 17]
    cap: 16
    len: 14
DIRECT ASSIGNMENT OF LAST ELEMENT (13°) STATUS_____
    slice [0 0 0 0 0 0 0 11 12 13 14 15 16 99]
    cap: 16
    len: 14

inside the UPDATE Slice POINTER function we have a sli pointer and we can modify directly the         structures appending elements = &[0 0 0 0 0 0 0 11 12 13 14 15 16 99 100 101 102]

but to access the values and modify it we have to create a local instance

*** Finally, passing the slice as a pointer will modify the sctructure: 
    slice [0 0 0 0 9 0 0 11 12 13 14 15 16 99 100 101 102]
    cap: 32
    len: 17

editSli
不起作用,因为您正在更改切片的本地副本的容量而不返回它。基本上,函数必须返回新的切片,原因与
append
必须返回更新的切片完全相同;因为如果它必须增加容量,它可能必须使用一个新的底层数组,因此任何对切片的旧引用都将无效。

您根本误解了切片是什么。切片是一个包含3个字段的结构:指向底层数组的指针、数据长度和底层数组的容量(相对于指针)。当你把一个切片传递给一个函数时,你就是在复制这个结构。它仍然引用底层数组,因此您对实际数据所做的更改将反映在函数范围之外,但对切片标头本身(包括数据长度)的更改不会反映出来,因为它是一个副本。如果需要修改切片,而不是底层数据,并将其反映到函数外部,请传入指向切片的指针

建议阅读:

START STATUS_____
slice [0 0 0 0 0 0 0 0]
    cap: 8
    len: 8

inside the edit Slice function the sli is = [0 0 0 0 0 0 0 11 33 34 35]
and the direct assignment of the last element (7°) ar right working.

However, the slice append will not be applied to the original slice because this is an internal copy
EDIT RESULT STATUS_____
    slice [0 0 0 0 0 0 0 11]
    cap: 8
    len: 8

inside the update Slice function the sli is =  [0 0 0 0 0 0 0 11 12 13 14]
and this append will be applied to the original one becouse of the return value of this function is a         new slice
UPDATE RESULT STATUS_____
    slice [0 0 0 0 0 0 0 11 12 13 14]
    cap: 16
    len: 11
DIRECT APPEND RESULT STATUS_____
    slice [0 0 0 0 0 0 0 11 12 13 14 15 16 17]
    cap: 16
    len: 14
DIRECT ASSIGNMENT OF LAST ELEMENT (13°) STATUS_____
    slice [0 0 0 0 0 0 0 11 12 13 14 15 16 99]
    cap: 16
    len: 14

inside the UPDATE Slice POINTER function we have a sli pointer and we can modify directly the         structures appending elements = &[0 0 0 0 0 0 0 11 12 13 14 15 16 99 100 101 102]

but to access the values and modify it we have to create a local instance

*** Finally, passing the slice as a pointer will modify the sctructure: 
    slice [0 0 0 0 9 0 0 11 12 13 14 15 16 99 100 101 102]
    cap: 32
    len: 17