How to import a struct that is inside of other pac

2020-07-04 06:03发布

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?

标签: struct go
1条回答
一夜七次
2楼-- · 2020-07-04 07:08

The LinkedList type is in the list namespace, so change your usage of the type to:

type SomeType struct {
    lst *list.LinkedList
}
查看更多
登录 后发表回答