Skip to content

affected: The custom struct with one field #52199

Closed
@hyper0x

Description

@hyper0x

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.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions