I tried to learn Go but I frequently feel frustrating because some basic features that other languages has seems not working in Go. So basically, I would like to use struct type that is define in other file. I was able to use functions except struct type. In main.go,
package main
import (
"list"
)
func main() {
lst := list.NewList(false)
lst.Insert(5)
lst.Insert(7)
lst.InsertAt(2, 1)
lst.PrintList()
}
This works perfectly (and all other functions) as I expect (list is in $GOPATH). In package list, I defined struct as follow:
type LinkedList struct {
head *node
size int
isFixed bool
}
I wanted to use this struct in other struct, so I attempted to do something like this,
type SomeType struct {
lst *LinkedList
}
But unfortunately, I got error that the type LinkedList is not defined. How can I use a struct that is defined in other package?
The
LinkedList
type is in thelist
namespace, so change your usage of the type to: