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

Confusing for these codes of interface implementation and calling.Someone please help. #28149

Closed
zeerax25 opened this issue Oct 11, 2018 · 1 comment

Comments

@zeerax25
Copy link

Please answer these questions before submitting your issue. Thanks!

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

  • go version go1.11 darwin/amd64

Does this issue reproduce with the latest release?

  • not testing

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

  • not related

What did you do?

If possible, provide a recipe for reproducing the error.
A complete runnable program is good.
A link on play.golang.org is best.

Code 1:

package main

import "fmt"

type myintf interface {
	GotU()
}

type esc struct {
	 i int
}

//func GotU(t esc);
func (e esc)GotU() {
	e.i = 10
}

func TestFunc(it myintf) string {
	it.GotU()
	return  "kk"
}

func main() {

	var test esc
	test.i = 9

	TestFunc(test)

	fmt.Println(test.i)

}

Code 2:

package main

import "fmt"

type myintf interface {
	GotU()
}

type esc struct {
	 i int
}

func (e esc)GotU() {
	e.i = 10
}

func TestFunc(it myintf) string {
	it.GotU()
	return  "kk"
}

func main() {

	var test esc
	test.i = 9

	TestFunc(test)

	fmt.Println(test.i)

}

Code 3:

package main

import "fmt"

type myintf interface {
	GotU()
}

type esc struct {
	 i int
}

func (e *esc)GotU() {
	e.i = 10
}

func TestFunc(it myintf) string {
	it.GotU()
	return  "kk"
}

func main() {

	var test esc
	test.i = 9

	TestFunc(test)

	fmt.Println(test.i)

}

What did you expect to see?

  • nothing to expect

What did you see instead?

code 1 output: 9
code 2 output: 9
code 3 cannot be compiled due to a type mismatch

Since only func (e esc)GotU() implemented, why should both pieces of code work and deliver the same result?
It's kind of confusing for me to pass a pointer of struct to that function (TestFunc) to get the same answer.

@ALTree
Copy link
Member

ALTree commented Oct 11, 2018

Hi,

the Go project does not use its bug tracker for general discussion or asking questions about the language. The Github bug tracker is only used for tracking bugs and proposals going through the Proposal Process.

Please see the Questions wiki page; it has a list of good places for asking questions about the language. Thanks!

Closing, since this is not a bug.

@ALTree ALTree closed this as completed Oct 11, 2018
@golang golang locked and limited conversation to collaborators Oct 11, 2019
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