Import struct from another package and file golang

huangapple go评论80阅读模式
英文:

Import struct from another package and file golang

问题

我在尝试从另一个包和文件中导入一种类型时遇到了问题。我想要导入的结构如下所示:

type PriorityQueue []*Item

type Item struct {
   value string
   priority int   
   index int 
}

如果我将PriorityQueue与其所有方法放在同一个文件中,我会这样声明它:

pq:= &PriorityQueue{}

我已经像疯子一样在互联网上搜索了这个简单问题的答案,但我没有找到答案。我通常使用Java编程,导入类是如此基础的操作。

英文:

I have a problem trying to import a type from another package and file. The struct that I'm trying to import is the one underneath.

type PriorityQueue []*Item

type Item struct {
   value string
   priority int   
   index int 
}

If I would put the PriorityQueue alongside with all of its methods in the same file I'd declare it with

pq:= &PriorityQueue{}

I've been searching the internet like a madman for an answer on this simple question but I have not found an answer. I usually program in java and import classes is so elementary.

答案1

得分: 97

在Go语言中,你不会导入类型或函数,而是导入包(参见规范:导入声明)。

以下是一个导入声明的示例:

import "container/list"

通过导入一个包,你可以访问它的所有导出标识符,并且可以使用包名.标识符名来引用它们,例如:

var mylist *list.List = list.New()

// 或者简写为:
l := list.New()

在导入声明中,还有一些技巧,例如:

import m "container/list"

这样你可以使用m.标识符名来引用导出的标识符,例如:

l := m.New()

还可以通过以下方式:

import . "container/list"

这样你可以完全省略包名:

l := New()

但只在紧急情况下或存在名称冲突时使用这些技巧(这种情况很少见)。

英文:

In Go you don't import types or functions, you import packages (see Spec: Import declarations).

An example import declaration:

import "container/list"

And by importing a package you get access to all of its exported identifiers and you can refer to them as packagename.Identifiername, for example:

var mylist *list.List = list.New()

// Or simply:
l := list.New()

There are some tricks in import declaration, for example by doing:

import m "container/list"

You could refer to the exported identifiers with "m.Identifiername", e.g.

l := m.New()

Also by doing:

import . "container/list"

You can leave out the package name completely:

l := New()

But only use these "in emergency" or when there are name collisions (which are rare).

答案2

得分: 16

@icza上面说的加上:

在Go 1.9中,有一种类型别名的方式,允许你从包中导入类型,并将它们别名为看起来像是本地类型:

package.go的内容:

type A struct {
    X, Y int
}

main.go的内容:

...

import myTypes "path/to/package"

// 注意等号(不是空格)
// 它不会创建一个新的“子类”
// 它是一个真正的本地别名。
// 允许你避免整体导入 `import . "path/to/package"`,它会将该包中的所有对象导入到本地作用域中。
type A = myTypes.A

...
英文:

What @icza said above plus:

With Go 1.9 there are type aliases that allow you to import types from packages and alias them into what look like local types:

package.go contents:

type A struct {
    X, Y int
}

main.go contents:

...

import myTypes "path/to/package"

// Note the equal sign (not empty space)
// It does NOT create a new "subclass"
// It's an actual alias that is local.
// Allows you to avoid whole-sale `import . "path/to/package"` which imports all objects from there into local scope.
type A = myTypes.A

...

答案3

得分: 15

此外,如果你想要导出一个结构体并在其包之外初始化该结构体的值,那么结构体的所有字段都必须以大写字母开头,否则你将会得到一个错误信息:"未导出的字段 'fieldName' 的使用"

type Item struct {
   Value string  // 大写字母 V
   Priority int  // 大写字母 P
   Index int     // 大写字母 I
}

感谢 @Vasantha Ganesh 的评论。

英文:

Also, if you want to export a struct and init this struct value outside of it's package than all fields of the struct must start with capital letter, otherwise you will get an error "Unexported field 'fieldName' usage"

type Item struct {
   Value string  // uppercase V
   Priority int  // uppercase P
   Index int     // uppercase I
}

Thanks to @Vasantha Ganesh comment

huangapple
  • 本文由 发表于 2015年4月27日 22:22:36
  • 转载请务必保留本文链接:https://go.coder-hub.com/29898400.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定