[英]Go package with multiple files, how to structure
Go noob, I cannot seem to figure out how to structure my project with packages. 新手,我似乎无法弄清楚如何使用包来构造我的项目。 What I want is this: 我想要的是:
dart.go dart.go
package main
import (
...
)
func main () {
...
// call functions declared in functions.go
...
}
functions.go functions.go
package dart
import (
...
)
func Function1() {
...
}
func Function2() {
...
}
if all you want to do is access functions in a different file, have functions.go also start with package main
instead of package dart
. 如果您要做的只是访问其他文件中的函数,请使用functions.go也从package main
而不是package dart
package main
开始。 That way, you are working within a single package but with your code divided into multiple files. 这样,您就可以在一个程序包中工作,但是将代码分为多个文件。 Make sure they're in the same directory so that they're considered in the same package. 确保它们在同一目录中,以便将它们考虑在同一软件包中。
As Wombologist mentioned, you can split different files under the same package without problem, assuming they share the same package
definition. 正如Wombologist所提到的,假设它们共享相同的package
定义,则可以在同一包下拆分不同的文件而不会出现问题。 The only exception for different package definitions under the same directory is for tests, where the recommended way of defining the package is adding _test
on it (eg package main_test
or package dart_test
). 对于相同的目录下不同的包定义唯一的例外是用于测试,其中限定所述包的推荐的方法是添加_test
在其上(例如package main_test
或package dart_test
)。
Not sure if it's your case here or if you are just experimenting with the language, but I would add that Go projects are easier to maintain when you group same domain code under packages and share them around, increasing the potential reusability. 不知道这是您的情况还是只是在尝试使用该语言,但是我想补充一下,当您将相同的域代码分组并打包共享时,Go项目更易于维护,从而增加了潜在的可重用性。
If you are just looking to create a library then you won't need a main package. 如果您只是想创建一个库,则不需要主包。 However, if you are looking to create a standalone program that runs functions from a different package ( dartlib
) then you'll need a main file. 但是,如果您要创建一个运行其他程序包( dartlib
)中的功能的独立程序,则需要一个主文件。
It would also be a good idea to name your program something different than the library you are calling ( program dart
calling library dartlib
) 给您的程序命名不同于正在调用的库( program dart
调用库 dartlib
)也是一个好主意。
Library 图书馆
Your library directory structure should look something like the following: 您的库目录结构应类似于以下内容:
dartlib
|
dartlib.go
dartlib.go dartlib.go
package dartlib
function Hello() string { return "hello!" }
This can be imported as the following: "github.com/your_github_username/dartlib"
可以将其导入如下: "github.com/your_github_username/dartlib"
Program 程序
Or, you can store the package within your program directory. 或者,您可以将包存储在程序目录中。 In this case the directory structure should look something like the following: 在这种情况下,目录结构应类似于以下内容:
dart (you_program_name)
|
dart.go
dartlib (package)
|
dartlib.go
In this case, the library can be imported as the following: "github.com/your_github_username/dart/dartlib"
在这种情况下,可以按以下方式导入该库: "github.com/your_github_username/dart/dartlib"
dart.go dart.go
package main
import (
"github.com/your_github_username/dart/dartlib"
"fmt"
)
helloString := dartlib.Hello()
fmt.Println(helloString)
go build .
at directory root produces the dart
executable. 在目录根目录下生成dart
可执行文件。
$./dart hello!
For more examples and further explanation see the docs 有关更多示例和更多说明,请参阅文档
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.