Skip to content

affected: The custom struct with one field #52199

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

Closed
hyper0x opened this issue Apr 7, 2022 · 1 comment
Closed

affected: The custom struct with one field #52199

hyper0x opened this issue Apr 7, 2022 · 1 comment

Comments

@hyper0x
Copy link

hyper0x commented Apr 7, 2022

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

$ go version
go version go1.18 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
GO111MODULE="on"
GOARCH="arm64"
GOBIN="/Users/XXX/Bin"
GOCACHE="/Users/XXX/Library/Caches/go-build"
GOENV="/Users/XXX/Library/Application Support/go/env"
GOEXE=""
GOEXPERIMENT=""
GOFLAGS=""
GOHOSTARCH="arm64"
GOHOSTOS="darwin"
GOINSECURE=""
GOMODCACHE="/Users/XXX/Golib/pkg/mod"
GONOPROXY=""
GONOSUMDB=""
GOOS="darwin"
GOPATH="/Users/XXX/Golib"
GOPRIVATE=""
GOPROXY="https://goproxy.cn,https://goproxy.io,direct"
GOROOT="/opt/homebrew/cellar/go/1.18/libexec"
GOSUMDB="sum.golang.org"
GOTMPDIR=""
GOTOOLDIR="/opt/homebrew/cellar/go/1.18/libexec/pkg/tool/darwin_arm64"
GOVCS=""
GOVERSION="go1.18"
GCCGO="gccgo"
AR="ar"
CC="clang"
CXX="clang++"
CGO_ENABLED="1"
GOMOD="/dev/null"
GOWORK=""
CGO_CFLAGS="-g -O2"
CGO_CPPFLAGS=""
CGO_CXXFLAGS="-g -O2"
CGO_FFLAGS="-g -O2"
CGO_LDFLAGS="-g -O2"
PKG_CONFIG="pkg-config"
GOGCCFLAGS="-fPIC -arch arm64 -pthread -fno-caret-diagnostics -Qunused-arguments -fmessage-length=0 -fdebug-prefix-map=/var/folders/ts/7lg_tl_x2gd_k1lm5g_48c7w0000gn/T/go-build2766836812=/tmp/go-build -gno-record-gcc-switches -fno-common"

What did you do?

My code:

package main

import (
	"fmt"
)

func main() {
	a := A{}
	m("A", a, SetAI(&a))
	b := B{}
	m("B", b, SetBI(&b))
}

type A struct {
	I int
	S string
}

type B struct {
	I int
}

func SetAI(a *A) A {
	a.I = 10
	return *a
}

func SetBI(b *B) B {
	b.I = 10
	return *b
}

func m(name string, arg1, arg2 interface{}) {
	fmt.Printf("%s: %v, %v\n", name, arg1, arg2)
}

Output:

A: {0 }, {10 }
B: {10}, {10}

What did you expect to see?

A: {0 }, {10 }
B: {0}, {10}

What did you see instead?

A: {0 }, {10 }
B: {10}, {10}

Why is the number in both curly braces in the output both 10 when the struct has only one field? Or, why is the value of field I different when the number of fields in the struct is different? Such an output is counter-intuitive.

@seankhliao
Copy link
Member

Duplicate of #52146

@seankhliao seankhliao marked this as a duplicate of #52146 Apr 7, 2022
@golang golang locked and limited conversation to collaborators Apr 7, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

3 participants