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

build: the struct embeds the inteface #55850

Closed
SimFG opened this issue Sep 24, 2022 · 2 comments
Closed

build: the struct embeds the inteface #55850

SimFG opened this issue Sep 24, 2022 · 2 comments

Comments

@SimFG
Copy link

SimFG commented Sep 24, 2022

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

$ go version
go version go1.18.6 darwin/amd64

Does this issue reproduce with the latest release?

the detail code

type A interface {
	A()
	//OtherA()
}

type a struct {
}

func (a *a) A() {
	fmt.Println("a")
}

func (a *a) OtherA() {
	fmt.Println("otherA")
}

type B interface {
	A
	Other()
}

type b struct {
	A
}

func (b *b) Other() {
	fmt.Println("other")
}

func PrintAB(b B) {
	b.A()
	//b.OtherA()
	b.Other()
}

func main() {
	PrintAB(&b{A: &a{}})
}

build the code, output:

./main.go:44:10: cannot use &b{…} (value of type *b) as type B in argument to PrintAB:
        *b does not implement B (missing A method)

The editor does not prompt syntax errors, but this code doesn't compile. If I change the A method to the OtherA in the A interface, it can compile successfully.

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

go env Output
$ go env

What did you do?

What did you expect to see?

What did you see instead?

@ianlancetaylor
Copy link
Member

The type *b has a field named A. That is, (*b).A refers to the field A. It's true that that that field has a method A that could be promoted to the type *b, but the field A shadows the method A.

In general we do not use the issue tracker for questions about the language. If you want to discuss this further, please use a forum; see https://go.dev/wiki/Questions. Thanks.

@ianlancetaylor ianlancetaylor closed this as not planned Won't fix, can't repro, duplicate, stale Sep 25, 2022
@SimFG
Copy link
Author

SimFG commented Sep 25, 2022

@ianlancetaylor thanks your answer >v<

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