在Go中使用在单独文件中定义的代码。

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

Using code defined in separate file in Go

问题

在一个项目中,如果我有代码分散在多个文件中,有没有其他方法可以在项目的文件中引用这些代码,而不需要进行整个构建和安装过程?例如,在同一个项目中,我可能在文件A中有一个类型Foo的实现,而我可能想在文件B中使用这个类型。有没有办法做到这一点?

英文:

Say I have got code for a project spread across multiple files. Is there any other way to reference such code within files in the project without going through the whole build and install process? For example within a single project, I might have an implementation of a type Foo in file A and I might want to use such type in file B within the same project. Is there anyway to go about doing that

答案1

得分: 3

我不清楚你具体在问什么,但是关于“声明和作用域”的语言规范的这部分可能与此相关或有帮助。另外,更加平凡的相关信息来源可能是“如何编写Go代码”。

编辑 - 对于现在扩展的问题进行扩展:

如果“包a”声明了类型Foo - 那么,因为Foo以大写字母开头 - Fooa“导出”。然后在“包b”中,可以写import "a",然后在“包b”中可以将该类型称为a.Foo

英文:

It's not clear to me what exactly you're asking about, but this part of the language specs about "Declarations and scope" might be related and/or helpful. Another, more prosaic source of related information might probably be "How to Write Go Code".

EDIT - Expanding on now expanded question:

If "package a" declares type Foo - then, b/c Foo starts with an upper case letter - Foo is exported by a. Then in "package b" one would write import "a" and can then refer to that type as a.Foo inside "package b".

huangapple
  • 本文由 发表于 2012年12月15日 01:45:45
  • 转载请务必保留本文链接:https://go.coder-hub.com/13883743.html
匿名

发表评论

匿名网友

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

确定