Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

builtin: append will change addr of element in slice ? #71169

Closed
zhuliquan opened this issue Jan 8, 2025 · 3 comments
Closed

builtin: append will change addr of element in slice ? #71169

zhuliquan opened this issue Jan 8, 2025 · 3 comments

Comments

@zhuliquan
Copy link

Go version

go version go1.23.2 linux/amd64

Output of go env in your module/workspace:

GO111MODULE='on'
GOARCH='amd64'
GOBIN=''
GOCACHE='/home/zhuliquan/.cache/go-build'
GOENV='/home/zhuliquan/.config/go/env'
GOEXE=''
GOEXPERIMENT=''
GOFLAGS=''
GOHOSTARCH='amd64'
GOHOSTOS='linux'
GOINSECURE=''
GOMODCACHE='/data/home/zhuliquan/workspace/go_workspace/pkg/mod'
GOOS='linux'
GOPATH='/data/home/zhuliquan/workspace/go_workspace'
GOPROXY='https://goproxy.cn,direct'
GOROOT='/data/home/zhuliquan/infra/go'
GOSUMDB='sum.golang.org'
GOTMPDIR=''
GOTOOLCHAIN='auto'
GOTOOLDIR='/data/home/zhuliquan/infra/go/pkg/tool/linux_amd64'
GOVCS=''
GOVERSION='go1.23.2'
GODEBUG=''
GOTELEMETRY='local'
GOTELEMETRYDIR='/home/zhuliquan/.config/go/telemetry'
GCCGO='gccgo'
GOAMD64='v1'
AR='ar'
CC='gcc'
CXX='g++'
CGO_ENABLED='1'
GOMOD='/dev/null'
GOWORK=''
CGO_CFLAGS='-O2 -g'
CGO_CPPFLAGS=''
CGO_CXXFLAGS='-O2 -g'
CGO_FFLAGS='-O2 -g'
CGO_LDFLAGS='-O2 -g'
PKG_CONFIG='pkg-config'
GOGCCFLAGS='-fPIC -m64 -pthread -Wl,--no-gc-sections -fmessage-length=0 -ffile-prefix-map=/tmp/go-build2218099817=/tmp/go-build -gno-record-gcc-switches'

What did you do?

I write a function to extract element from a slice, this function will return a element and a slice which contains other element.

func partSlice[T any](l []T, idx int) (T, []T) {
    return l[idx], append(l[:idx], l[idx+1:]...)
}

What did you see happen?

I write below case to invoke partSlice func.

package main

import (
        "fmt"
)

func main() {
        l := []int{1, 2}
        fmt.Printf("l addr: %p\n", l)
        k, s := partSlice(l, 0)
        fmt.Printf("k: %d, s: %v, l: %v\n", k, s, l)
        fmt.Printf("s addr: %p\n", s)
        fmt.Printf("l addr: %p\n", s)
}

func partSlice[T any](l []T, idx int) (T, []T) {
        return l[idx], append(l[:idx], l[idx+1:]...)
}

the result is

l addr: 0xc000012110
k: 2, s: [2], l: [2 2]
s addr: 0xc000012110
l addr: 0xc000012110

I am confuse that k is 2, I expect it is 1. And l is replace to [2 2]!
According to addr of s and l. append(l[idx], l[idx+1]...) make 1th and 2nd elment have same addr?

What did you expect to see?

expect got result is 1 [2]

@zhuliquan zhuliquan changed the title builtin: append will change addr of slice ? builtin: append will change addr of element in slice ? Jan 8, 2025
@randall77
Copy link
Contributor

This is behaving as expected.
The append is happening before the evaluation of l[idx]. (They could happen in either order according to https://go.dev/ref/spec#Order_of_evaluation.)

append will modify the existing slice's backing store - it does not allocate a new slice. You might want to read https://go.dev/blog/slices-intro if you haven't already.

@zigo101
Copy link

zigo101 commented Jan 8, 2025

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants