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

Setting fields via reflect.Value.Index.Set works on slices but panics on arrays #39094

Closed
dany74q opened this issue May 15, 2020 · 2 comments
Closed

Comments

@dany74q
Copy link

@dany74q dany74q commented May 15, 2020

What version of Go are you using (go version)?

$ go version
go version go1.14.2 darwin/amd64

Does this issue reproduce with the latest release?

Yes

What operating system and processor architecture are you using (go env)?

go env Output
$ go env
GO111MODULE="on"
GOARCH="amd64"
GOBIN=""
GOCACHE="/Users/dany74q/Library/Caches/go-build"
GOENV="/Users/dany74q/Library/Application Support/go/env"
GOEXE=""
GOFLAGS=""
GOHOSTARCH="amd64"
GOHOSTOS="darwin"
GOINSECURE=""
GOOS="darwin"
GOPATH="/Users/dany74q/go"
GOPROXY="https://proxy.golang.org,direct"
GOROOT="/usr/local/Cellar/go/1.14.2_1/libexec"
GOSUMDB="sum.golang.org"
GOTMPDIR=""
GOTOOLDIR="/usr/local/Cellar/go/1.14.2_1/libexec/pkg/tool/darwin_amd64"
GCCGO="gccgo"
AR="ar"
CC="clang"
CXX="clang++"
CGO_ENABLED="1"
GOMOD="/dev/null"
CGO_CFLAGS="-g -O2"
CGO_CPPFLAGS=""
CGO_CXXFLAGS="-g -O2"
CGO_FFLAGS="-g -O2"
CGO_LDFLAGS="-g -O2"
PKG_CONFIG="pkg-config"
GOGCCFLAGS="-fPIC -m64 -pthread -fno-caret-diagnostics -Qunused-arguments -fmessage-length=0 -fdebug-prefix-map=/var/folders/vz/n93rj9qs3mlb902f5z9b99ph0000gn/T/go-build920636391=/tmp/go-build -gno-record-gcc-switches -fno-common"

What did you do?

I was trying to set an array's element via reflection:

func main() {
	valuesArr := [1]int{1}
	valuesSlice := []int{1}

	// Works
	valuesArr[0] = 2
	valuesSlice[0] = 2

	newVal := reflect.ValueOf(3)

	// Works
	reflect.ValueOf(valuesSlice).Index(0).Set(newVal)

	// Panics
	reflect.ValueOf(valuesArr).Index(0).Set(newVal)
}

panic: reflect: reflect.flag.mustBeAssignable using unaddressable value

goroutine 1 [running]:
reflect.flag.mustBeAssignableSlow(0x16)
        /usr/local/Cellar/go/1.14.2_1/libexec/src/reflect/value.go:247 +0x19d
reflect.flag.mustBeAssignable(0x16)
        /usr/local/Cellar/go/1.14.2_1/libexec/src/reflect/value.go:234 +0x4c
reflect.Value.Set(0x1a9dce0, 0xc0003a4158, 0x16, 0x1a9dce0, 0xc0003a4158, 0x16)
        /usr/local/Cellar/go/1.14.2_1/libexec/src/reflect/value.go:1526 +0x32
main.main()
        /Users/dany74q/go/src/github.com/beyondnetworks/beyond/cloudplatform-dal/cmd/main.go:42 +0x321
Exiting.

https://play.golang.org/p/SYRoed00w6C

What did you expect to see?

I expected to see a consistent behavior when operating on arrays and slices - that is, either both producing a panic, or both working.

What did you see instead?

Setting the element value works on slices, but fails on arrays (of unaddressable types).

@seankhliao
Copy link
Contributor

@seankhliao seankhliao commented May 15, 2020

Go's pass by value semantics mean your array is copied when passed to reflect.
You instead want to pass a pointer to the array and dereference that to be able to set values.

reflect.ValueOf(&valuesArr).Elem().Index(0).Set(newVal)
@dany74q
Copy link
Author

@dany74q dany74q commented May 15, 2020

A-ha !

Makes sense, thank you @seankhliao.

@dany74q dany74q closed this May 15, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Linked pull requests

Successfully merging a pull request may close this issue.

None yet
2 participants
You can’t perform that action at this time.