How to use list of struct retuned from other pkg in main.go 's interface?

Hi All,

!Newbie Alert!
I have been struggling to figure out this for sometime. I am not sure if this is the right approach to do in golang or not.

main.go:

https://play.golang.org/p/Ej3lbIEqSEn

helper.go @ modules/helper.go … which is getting imported in main.go:

https://play.golang.org/p/datdSfrIyXP


Error:

cannot use n1 (type []modules.Info) as type Infoer in assignment:
[]modules.Info does not implement Infoer (missing GetName method)

I understand why error is coming. I am not able to come up with a solution to it.

Any help would be highly appreciated.

Thanks!

for _, i := range n1 {
   fmt.Println(i.GetName())
}

Charles,

Thank you very much for response.

I am mainly trying to understand if there is any possibility to use interface in main.go on n1? Or i am thinking really crazy?

In Python world, i could get any kind of data from module and send it to function as long as it’s parameter accepts it.

Package main can’t be imported into other packages, therefore you can not reference any or use any type or function defined in it.

So you need to move these types into a package that you then import into main and your other package.

1 Like

Thanks Norbert.

I am trying to import from “modules” pkg. Here is how main.go and modules/helper.go look like:

path main.go:

package main

import (
	"fmt"
	"interface_hello_1/modules"
)

type Infoer interface {
	GetName() []Info
}

type Info struct {
	Name string
	Age  int
}

func (i Info) GetName() []Info {
	Infos := []Info{
		{Name: "Foo", Age: 3},
	}
	return Infos
}

func infoPrint(i Infoer) {
	fmt.Println(i.GetName())
}

func main() {
	in := Info{}
	var i Infoer = in
	fmt.Println(i.GetName()) // Works

	// From modules

	n1 := modules.Info{}.GetName()
	// Need guidance here... to make use of interface
	var i1 Infoer = n1
	i1.GetName()
}

path modules/helper.go:

package modules

type Info struct {
	Name string
	Age  int
}

func (t Info) GetName() []Info {
	Infos := []Info{
		{Name: "Bar", Age: 4},
	}
	return Infos
}

The main.Info is a different type then modules.Info you can not swap them out for each other.

Proper way is to implement the interface I. modules and use modules.Info where appropriate in main.

Thanks Norbert. I am trying to implement interface for modules. No success so far :frowning:

Hopefully soon i will figure out. :slight_smile:

You can’t implement an interface for a module, only for a type.

Tried doing something like below; It is working. I am not quite sure if this is right way to go or there can be some more elegant way to do in GO.

path main.go:

package main

import (
	"fmt"
	"interface_hello_1/modules"
)

// Infoer Interface
type Infoer interface {
	GetName() []Info
}

// Info struct to hold Name & Age; Implements Infoer interface
type Info struct {
	Name string
	Age  int
}

// GetName Method
func (i Info) GetName() []Info {
	Infos := []Info{
		{Name: i.Name, Age: i.Age},
	}
	return Infos
}

func infoPrint(i Infoer) {
	fmt.Println(i.GetName())
}

func main() {
	in := Info{Name: "Foo", Age: 3}
	var i Infoer = in
	fmt.Println(i.GetName()) // Works

	// From modules

	n1 := modules.Info{}.GetName()
	var in1 Info
	for _, item := range n1 {
		in1.Name = item.Name
		in1.Age = item.Age
	}
	var i1 Infoer = in1
	fmt.Println(i1.GetName())
}

path modules/helper.go:

package modules

type Info struct {
	Name string
	Age  int
}

func (t Info) GetName() []Info {
	Infos := []Info{
		{Name: "Bar", Age: 4},
	}
	return Infos
}

Output:

$ go run main.go
[{Foo 3}]
[{Bar 4}]

Both types are different, and they also do not share a common interface.

main.Infoer requires the type to implement a method GetName() which returns something of type []main.Info, though modules.Info.GetName() does return []modules.Info.

So let me say it again, you need to extract the types and interfaces from main and move them to another package, modules or somewhere else.

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.