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

interface and func as parameters #52146

Closed
EvenBoom opened this issue Apr 4, 2022 · 3 comments
Closed

interface and func as parameters #52146

EvenBoom opened this issue Apr 4, 2022 · 3 comments

Comments

@EvenBoom
Copy link

EvenBoom commented Apr 4, 2022

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

$ go version
go version go1.18 windows/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
set GO111MODULE=
set GOARCH=amd64
set GOBIN=D:\GoFuture\bin
set GOCACHE=C:\Users\乐谱里的画\AppData\Local\go-build
set GOENV=C:\Users\乐谱里的画\AppData\Roaming\go\env
set GOEXE=.exe
set GOEXPERIMENT=
set GOFLAGS=
set GOHOSTARCH=amd64
set GOHOSTOS=windows
set GOINSECURE=
set GOMODCACHE=D:\GoFuture\pkg\mod
set GONOPROXY=
set GONOSUMDB=
set GOOS=windows
set GOPATH=D:\GoFuture
set GOPRIVATE=
set GOPROXY=https://proxy.golang.org,direct
set GOROOT=C:\Program Files\Go
set GOSUMDB=sum.golang.org
set GOTMPDIR=
set GOTOOLDIR=C:\Program Files\Go\pkg\tool\windows_amd64
set GOVCS=
set GOVERSION=go1.18
set GCCGO=gccgo
set GOAMD64=v1
set AR=ar
set CC=gcc
set CXX=g++
set CGO_ENABLED=1
set GOMOD=NUL
set GOWORK=
set CGO_CFLAGS=-g -O2
set CGO_CPPFLAGS=
set CGO_CXXFLAGS=-g -O2
set CGO_FFLAGS=-g -O2
set CGO_LDFLAGS=-g -O2
set PKG_CONFIG=pkg-config
set GOGCCFLAGS=-m64 -mthreads -fno-caret-diagnostics -Qunused-arguments -fmessage-length=0 -fdebug-prefix-map=C:\Users\ 乐谱里~1\AppData\Local\Temp\go-build1467603294=/tmp/go-build -gno-record-gcc-switches

What did you do?

The results are different when struct A has only one field and B has two or above fields, and it just happens when parameter type is interface.
I'm not sure if that's a bug.
Please clear my doubts, tx!

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.Println(name+":", arg1, arg2)
}
``` golang


### What did you expect to see?
A: {10} {10}
B: {10} {10}


### What did you see instead?
A: {0 } {10 }
B: {10} {10}
@ianlancetaylor
Copy link
Contributor

In a function call like m("A", a, SetAI(&a)) the Go language does not specify whether the second argument or the third argument is evaluated first. This code could be equivalent to

    t1 := a
    t2 := SetAI(&a)
    m("A", t1, t2)

or it could be equivalent to

    t2 := SetAI(&s)
    t1 := a
    m("A", t1, t2)

Different compilers can produce different results. Even different versions of the same compiler can produce different results.

In general we do not use the issue tracker for questions. If you have further questions about Go please use a forum. See https://go.dev/wiki/Questions. Thanks.

@go101
Copy link

go101 commented Apr 8, 2022

related issues:

The current issue is for function argument passing.

In fact, result returning and argument passing are also value assignments.

@EvenBoom
Copy link
Author

I got it, thanks everyone!

@golang golang locked and limited conversation to collaborators Apr 16, 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

4 participants