Go语言和命名空间:是否可以实现类似Python的功能?

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

Go and namespaces: is it possible to achieve something similar to Python?

问题

我想知道在Go语言中是否有一种类似Python的方式来使用命名空间。

在Python中,如果我有以下包含函数的文件:

/a.py
    def foo():
/b.py
    def bar():

我可以在第三个Python文件中按以下方式访问foobar

import a
a1 = a.foo()
import b
b1 = b.bar()

我在Go语言中找到了一些关于命名空间的文档方面的困难。在Go语言中,命名空间是如何实现的?是通过packageimport吗?还是import专门用于外部库?

我认为我理解了每个包应该有一个专用的目录。我想知道这是否绝对必要,因为每个包使用一个目录可能在需要高粒度模块化的情况下变得不实用。换句话说,我想避免每个包使用一个目录(用于内部使用)。

英文:

I wonder if there is a way to use namespaces in the Go language in a similar way as Python does.

In Python if I have the following files containing functions:

/a.py
    def foo():
/b.py
    def bar():

I can access foo and bar in a third Python file as following:

import a
a1 = a.foo()
import b
b1 = b.bar()

I had some difficulties finding some documentation on namespaces with the Go language.
How are namespaces implemented in go? With package and import? Or is import dedicated to external libraries?

I think I understood that for each package there should be a dedicated directory. I wonder if this is absolutely mandatory, because it can become impractical whenever a high granularity of modules is the best way to engineer a certain idea. In other words, I would like to avoid using one directory per package (for internal use).

答案1

得分: 25

Go语言中与Python模块相对应的是包(packages)。不同于Python模块只存在一个文件,Go的包由一个目录表示。一个简单的包可能只包含目录中的一个源文件,而较大的包可以分成多个文件。

所以,以你的Python示例为例,你可以创建一个名为$GOPATH/src/a/a.go的文件,内容如下:

package a

import "fmt"

func Foo() {
    fmt.Println("a.Foo")
}

在你的主程序中,你可以这样调用该函数:

package main

import "a"

func main() {
    a.Foo()
}

需要注意的是,只有在a包中以大写字母开头的导出类型和函数才能在外部使用(这也是我将foo重命名为Foo的原因)。

如果你不想设置一个Go工作空间,你也可以使用相对路径进行导入。例如,如果你将主程序中的导入语句改为import "./a",那么a包的源代码可以相对于主程序位于a/a.go的位置。

英文:

Go's equivalent to Python modules are packages. Unlike Python modules that exist as a single file, a Go package is represented by a directory. While a simple package might only include a single source file in the directory, larger packages can be split over multiple files.

So to take your Python example, you could create a file $GOPATH/src/a/a.go with the following contents:

package a

import "fmt"

func Foo() {
    fmt.Println("a.Foo")
}

In your main program, you can call the function like so:

package main

import "a"

func main() {
    a.Foo()
}

One thing to note is that only exported types and functions in a will be available externally. That is, types and functions whose names begin with a capital letter (which is why I renamed foo to Foo).

If you don't want to set up a Go workspace, you can also use relative paths for your imports. For instance, if you change the import in the main program to import "./a", then the source code for the a package can be located at a/a.go relative to the main program.

huangapple
  • 本文由 发表于 2014年4月10日 23:01:15
  • 转载请务必保留本文链接:https://go.coder-hub.com/22991798.html
匿名

发表评论

匿名网友

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

确定