如何在同一包中的不同文件中访问未导出的函数?

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

How to access unexported functions in the same package but from different file

问题

我正在尝试在本地构建godoc.org的源代码以进行一些更改。我的工作目录是/Users/Dany/go/src/github.com/golang/gddo。在gddo-server包中有几个文件。其中一个Go文件使用了同一包中未导出的另一个文件中的函数。它抛出了Undefined: <function-name>异常。

文件夹结构如下:

golang/gddo/
              gddo-server
                        main.go
                        crawl.go

如何在不同的文件中使用同一包中未导出的函数?有人可以帮助我吗?还有,有人知道如何构建godoc.org的代码吗?

英文:

I am trying to build godoc.org source code in my local to make some changes. My working directory is /Users/Dany/go/src/github.com/golang/gddo. In gddo-server package there several files. One of the go file uses a function from another file which is in the same package but unexported. It is throwing Undefined: &lt;function-name&gt; exception.

Folder is structure is,

golang/gddo/
              gddo-server
                        main.go
                        crawl.go

How do we use unexported function from the same package in a different file? Could anyone help me with this. Also if anyone has any idea about how to build godoc.org code?

答案1

得分: 5

同一个包的源文件可以轻松地引用其他源文件中定义的标识符,前提是它们位于同一个文件夹并且具有相同的包声明。你可以像在一个文件中定义的那样引用所有包级别的导出和非导出标识符。

参见规范:包

一个包由一个或多个源文件构成,这些源文件一起声明了属于该包的常量、类型、变量和函数,并且在同一个包的所有文件中都可以访问。

以及规范:包声明

具有相同包名的一组文件构成了一个包的实现。实现可能要求一个包的所有源文件位于同一个目录中。

需要注意的一点是,你的示例似乎是特殊的main包。如果你想用go run运行它,你需要列举所有的源文件。

要用go run运行你的示例,请进入gddo-server文件夹并输入:

go run background.go browse.go client.go crawl.go graph.go main.go play.go template.go

或者,如果你首先构建它会更简单。进入gddo-server文件夹并输入:

go build

这将在同一个文件夹中生成一个本机可执行二进制文件。要运行它,请输入:gddo-server(在Windows上)或./gddo-server(在Linux上)。

或者,你可以使用go install进行安装,它会将结果可执行二进制文件放在$GOPATH/bin文件夹中。

英文:

Source files of the same package can refer to identifiers defined in any of the source files without any effort. If they are in the same folder and if they have the same package declaration, you can refer all package-level exported and unexported identifiers as if all would have been defined in one file.

See Spec: Packages:

> A package in turn is constructed from one or more source files that together declare constants, types, variables and functions belonging to the package and which are accessible in all files of the same package.

And Spec: Package clause:

> A set of files sharing the same PackageName form the implementation of a package. An implementation may require that all source files for a package inhabit the same directory.

One thing to note: your example seems to be the special main package. If you want to run it with go run, you have to enumerate all the source files.

To run your example with go run, navigate to the gddo-server folder and type:

go run background.go browse.go client.go crawl.go graph.go main.go play.go template.go 

Or simpler if you first build it. Navigate to the gddo-server folder and type:

go build

This will generate a native executable binary in the same folder. To run it type: gddo-server (on Windows) or ./gddo-server (on Linux).

Or you can install it with go install which will place the result executable binary in your $GOPATH/bin folder.

huangapple
  • 本文由 发表于 2015年10月7日 14:24:44
  • 转载请务必保留本文链接:https://go.coder-hub.com/32985137.html
匿名

发表评论

匿名网友

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

确定