如何导入其他包内部的结构? [英] How to import a struct that is inside of other package?

查看:45
本文介绍了如何导入其他包内部的结构?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我尝试学习Go,但是我经常感到沮丧,因为其他语言似乎无法在Go中使用某些基本功能.所以基本上,我想使用的结构类型是在其他文件中定义.我能够使用除结构类型以外的功能.在main.go中,

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()
  }

这和我期望的一样完美(以及所有其他功能)(列表在$ GOPATH中).在软件包列表中,我将struct定义如下:

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
}

但是不幸的是,我收到未定义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?

推荐答案

LinkedList 类型位于 list 名称空间中,因此将类型的用法更改为:/p>

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

type SomeType struct {
    lst *list.LinkedList
}

这篇关于如何导入其他包内部的结构?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆