简体   繁体   English

与多个文件一起打包,如何结构

[英]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: 我想要的是:

  • I want to create a package, lets say its called Dart. 我想创建一个包,可以说它叫做Dart。
  • I have a file called dart.go with package main and the main function in my project directory. 我的项目目录中有一个名为dart.go的文件,其中包含main和main函数。
  • I have another file, lets call it functions.go in my project directory with 'package dart' as the first line. 我有另一个文件,在我的项目目录中以“ package dart”作为第一行将其命名为functions.go。
  • I just want to call functions from functions.go in main, but cannot figure out how to name the packages to get it to build. 我只想从main中的functions.go调用函数,但无法弄清楚如何命名包以使其生成。
  • If I put package dart at the top of functions.go it wont build because it finds packages main and dart. 如果我将dart软件包放到functions.go的顶部,它将无法构建,因为它会找到main和dart软件包。 I dont want functions.go to be part of another package, I just want one package and the ability to split the functions in this package into multiple files. 我不希望functions.go成为另一个程序包的一部分,我只希望一个程序包以及将此程序包中的功能拆分为多个文件的功能。
  • Is this possible in go, or do I have to make multiple packages? 这是可行的,还是我必须制作多个包裹?

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_testpackage 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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM