英文:
in go, don't we need to import another file in same directory if we want to use function defined in that file?
问题
在Go语言中,如果我们想要使用同一目录下的另一个文件中定义的函数,是不是需要导入该文件呢?
例如:
FolderA-
-------- FileA.go
---------FileB.go
在FileB.go中,我定义了一个方法Foo()
在FileA.go中,我想要调用FileB.go中定义的Foo()方法。
我需要在FileA.go中导入FileB吗,像这样:
import "FileB"
英文:
in go, don't we need to import another file in same directory if we want to use function defined in that file?
For example.
FolderA-
-------- FileA.go
---------FileB.go
In FileB.go, I define method Foo()
In FileA.go, I want to call Foo() which defined in FileB.go.
Do I need to import FileB in FileA.go like this?
import ("FileB")
答案1
得分: 14
不。只需调用一个函数。请注意:
- 在Go中,你不是包含文件,而是包含包。
- 这些文件应该共享同一个包。
请查看这个链接:https://blog.golang.org/organizing-go-code
而且谷歌上也有很多好的信息,例如:http://thenewstack.io/understanding-golang-packages/
英文:
No. Just call a function. Please note:
- You are not including files in Go, you include packages.
- These files should share the package.
Check this: https://blog.golang.org/organizing-go-code
And google is full of good info as well, ex. http://thenewstack.io/understanding-golang-packages/
答案2
得分: 8
不,你不需要导入那些文件。
将一个目录中的所有.go
文件视为一个包,将其下的子目录视为另一个包。
你可以从https://talks.golang.org/2014/organizeio.slide#1了解更多信息。
因此,只有当你想要使用其他目录中的文件中的函数时,才需要导入。
例如,我们在一个名为fruit
的目录中有两个文件:
apple.go
package fruit
import(fmt)
func ExportedMethod() {
fmt.Print("apple")
}
func privateMethod() {}
banana.go
package fruit
import(fmt)
func banana() {
fmt.Print("banana")
ExportedMethod()
pivateMethod()
}
这两个文件在Go中被视为一个包,你可以从另一个文件中调用方法,即使该方法是未导出的(使用小写字母作为首字母)。
你可以在这里了解更多关于导出和未导出的信息:https://www.goinggo.net/2014/03/exportedunexported-identifiers-in-go.html
但是,banana.go
需要导入fmt
包,即使apple.go
已经导入了fmt
包,因为包中的依赖关系必须在使用它的每个文件中明确列出。
英文:
No you don't need to import those files.
Go treat all .go
file in one directory as one package and directory under it as another package.
you can learn more from https://talks.golang.org/2014/organizeio.slide#1
So you only need to import if you want to use another function that locate inside files in other directory.
for example we have 2 files inside one fruit
directory
apple.go
package fruit
import(fmt)
func ExportedMethod() {
fmt.Print("apple")
}
func privateMethod() {}
banana.go
package fruit
import(fmt)
func banana() {
fmt.Print("banana")
ExportedMethod()
pivateMethod()
}
those two files is treated as one package in Go and you can call method from another file even the method is unexported (using lowercase at first character)
you can learn more about exported and unexported here https://www.goinggo.net/2014/03/exportedunexported-identifiers-in-go.html
but banana.go
need to import fmt
package even thought apple.go
is already import the fmt
package because dependencies in package must be listed specific on each file that uses it.
答案3
得分: 3
不,你不需要导入包,只需调用你的方法。
package test
import "fmt"
func main() {
foo()
}
英文:
No you don't have to import pakages just call your method.
package test
import "fmt"
func main() {
foo()
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论