Internal organizing for golang package with many files

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

Internal organizing for golang package with many files

问题

我有一个Go库,我想要分发它。它有62个源文件,但我想将API保持在一个单独的包中。**有没有办法在一个包中使用多个目录来编写代码?**源文件本身并不多,因为它们都很小,如果可能的话,我希望保持这种方式以便于导航。

由于它是模仿其他语言中类似库的,目前它有两个包:/project和/project/models。在了解更多关于Go包的知识后,我意识到这种方式在分发和使用上不太方便。用户更希望有一个单独的包。

有没有一种“gopheric”的方法来实现这个?

英文:

I've got a Go library that I'd like to distribute. It's got 62 source files, but I'd like to keep the API in a single package. Is there a way I can use multiple directories for code in a single package? It's not a huge amount of source, as the source files themselves are small, and I'd like to keep it that way if possible to make it navigable.

Since it was modeled after similar libraries in other languages, It's currently got two packages: /project and /project/models. After learning more about Go packaging I now realize that this is unwieldy for distribution and use. The user would prefer a single package.

Is there a "gopheric" way of doing this?

答案1

得分: 5

阅读这两篇文章可以对最佳实践有一个很好的理解:

  1. Go在生产中的应用
  2. 组织Go代码

你的应用程序中,每个路径目录可以作为一个独立的模块/包:

github.com/myaccount/myapp/
	README.md
	Makefile
    applib/
        cmd/
    		main.go
    		main_test.go
    		handlers.go
    		handlers_test.go
    	lib1/
    		main.go
    		main_test.go
    		process.go
    		process_test.go
    	lib2/
    		foo.go
    		foo_test.go
    		bar.go
    		bar_test.go
英文:

Read this two articles to have a good understanding of the best practices:

  1. Go In Production
  2. Organizing Go code

Each path directory can be a separate module/package in your app:

github.com/myaccount/myapp/
	README.md
	Makefile
    applib/
        cmd/
    		main.go
    		main_test.go
    		handlers.go
    		handlers_test.go
    	lib1/
    		main.go
    		main_test.go
    		process.go
    		process_test.go
    	lib2/
    		foo.go
    		foo_test.go
    		bar.go
    		bar_test.go

答案2

得分: 3

不,它非常简单:一个目录等于一个包。但是,拥有一种带有子目录中的“辅助”包的“主”包并不真正“难以分发”,因为go get可以很好地处理这个问题。

英文:

No, it is dead simple: One directory == one package. But having some kind of "main" package with "helper" packages in subdirectories is not really "unwieldy for distribution" as go get handles this very well.

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

发表评论

匿名网友

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

确定