Skip to content

Cannot use interface methods when returned through function returning []Interface type #357

Open
@Jackenmen

Description

@Jackenmen

Reproduction steps:

  1. Run commands:
mkdir dum
cd dum
go mod init dummy.com/dum
mkdir hello
  1. Create file named hello/hello.go with following contents:
package hello

type Example interface {
    Sample() int
}

type ExampleImpl struct {
    sample int
}

func (example *ExampleImpl) Sample() int {
    return example.sample
}

func Hello() []Example {
    return []Example{&ExampleImpl{sample: 123}, &ExampleImpl{sample: 321}}
}
  1. Build with gopy:
gopy build -output=out dummy.com/dum/hello
  1. Run the following Python code:
>>> from out import hello
>>> output = hello.Hello()
>>> output[0].Sample()
panic: interface conversion: *hello.Example is not hello.Example: missing method Sample

goroutine 17 [running, locked to thread]:
main.hello_Example_Sample(0x2)
	/tmp/dum/out/hello.go:1230 +0xf9
zsh: IOT instruction (core dumped)  python

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions