英文:
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
以大写字母开头 - Foo
被a
“导出”。然后在“包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".
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论