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

encoding/binary: Read and Write should be functionally equivalent #54492

Closed
QianChenglong opened this issue Aug 17, 2022 · 1 comment
Closed

Comments

@QianChenglong
Copy link

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

$ go version
go version go1.19 darwin/arm64

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

What did you do?

func Test_struct(t *testing.T) {
	type S1 struct {
		age   uint8
		Class uint8
	}
	s1 := &S1{
		age:   255,
		Class: 1,
	}

	buf := new(bytes.Buffer)
	err := binary.Write(buf, binary.BigEndian, s1)
	assert.Nil(t, err)
	fmt.Printf("%#X\n", buf)

	s2 := new(S1)
	err = binary.Read(buf, binary.BigEndian, s2)
	assert.Nil(t, err)
	fmt.Printf("%#v\n", s2)
}

What did you expect to see?

Read should work as Write OR Write panic as Read

What did you see instead?

GOROOT=/opt/homebrew/Cellar/go/1.19/libexec #gosetup
GOPATH=/Users/chenglong/go #gosetup
/opt/homebrew/Cellar/go/1.19/libexec/bin/go test -c -o /private/var/folders/8r/llxlqhb519540gxrx8crdkx80000gn/T/GoLand/___1Test_struct_in_binary_test_go.test -gcflags all=-N -l /Users/chenglong/src/go-test/0.go/2.library/encoding/binary/binary_test.go #gosetup
/opt/homebrew/Cellar/go/1.19/libexec/bin/go tool test2json -t /private/var/folders/8r/llxlqhb519540gxrx8crdkx80000gn/T/dlvLauncher.sh /Users/chenglong/Library/Application Support/JetBrains/Toolbox/apps/Goland/ch-0/222.3345.118/GoLand.app/Contents/plugins/go/lib/dlv/macarm/dlv --listen=127.0.0.1:54072 --headless=true --api-version=2 --check-go-version=false --only-same-user=false exec /private/var/folders/8r/llxlqhb519540gxrx8crdkx80000gn/T/GoLand/___1Test_struct_in_binary_test_go.test -- -test.v -test.paniconexit0 -test.run ^\QTest_struct\E$
=== RUN Test_struct
0XFF01
--- FAIL: Test_struct (432.25s)
panic: reflect: reflect.Value.SetUint using value obtained using unexported field [recovered]
panic: reflect: reflect.Value.SetUint using value obtained using unexported field

goroutine 4 [running]:
testing.tRunner.func1.2({0x1042d37e0, 0x140000550a0})
/opt/homebrew/Cellar/go/1.19/libexec/src/testing/testing.go:1396 +0x374
testing.tRunner.func1()
/opt/homebrew/Cellar/go/1.19/libexec/src/testing/testing.go:1399 +0x560
panic({0x1042d37e0, 0x140000550a0})
/opt/homebrew/Cellar/go/1.19/libexec/src/runtime/panic.go:890 +0x26c
reflect.flag.mustBeAssignableSlow(0x1a8)
/opt/homebrew/Cellar/go/1.19/libexec/src/reflect/value.go:257 +0xf0
reflect.flag.mustBeAssignable(0x1a8)
/opt/homebrew/Cellar/go/1.19/libexec/src/reflect/value.go:247 +0x68
reflect.Value.SetUint({0x1042d39e0, 0x14000018be2, 0x1a8}, 0xff)
/opt/homebrew/Cellar/go/1.19/libexec/src/reflect/value.go:2331 +0x30
encoding/binary.(*decoder).value(0x1400007bd38, {0x1042d39e0, 0x14000018be2, 0x1a8})
/opt/homebrew/Cellar/go/1.19/libexec/src/encoding/binary/binary.go:650 +0x220
encoding/binary.(*decoder).value(0x1400011bd38, {0x1042e7100, 0x14000018be2, 0x199})
/opt/homebrew/Cellar/go/1.19/libexec/src/encoding/binary/binary.go:625 +0x660
encoding/binary.Read({0x10430ae28, 0x140000673e0}, {0x10430c3a8, 0x104459d68}, {0x1042cc5c0, 0x14000018be2})
/opt/homebrew/Cellar/go/1.19/libexec/src/encoding/binary/binary.go:326 +0x10a0
command-line-arguments.Test_struct(0x14000003ba0)
/Users/chenglong/src/go-test/0.go/2.library/encoding/binary/binary_test.go:28 +0x1a0
testing.tRunner(0x14000003ba0, 0x104309dc8)
/opt/homebrew/Cellar/go/1.19/libexec/src/testing/testing.go:1446 +0x174
created by testing.(*T).Run
/opt/homebrew/Cellar/go/1.19/libexec/src/testing/testing.go:1493 +0x540

Debugger finished with the exit code 0

@ianlancetaylor
Copy link
Contributor

That is how the language works: you can read unexported fields, but you can't write them.

@ianlancetaylor ianlancetaylor closed this as not planned Won't fix, can't repro, duplicate, stale Aug 17, 2022
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

2 participants