英文:
How can I overcome Golang's requirement that every package have buildable code in it?
问题
我正在使用Go构建一个Web应用程序,其中包含几个中间件函数。目前,它们都位于"my/middleware"包中。由于我定义了许多不同的函数,这个命名空间变得非常混乱,所以我决定将它们放在自己的子目录中,例如"my/middleware/gzip"。但是,当我这样做时,出现了以下错误:
no buildable Go source files my/middleware
我不想将所有这些函数放在同一个命名空间中,但似乎我的唯一选择是在"my/middleware"目录中创建一个占位的.go文件,其中包含一个空的init函数之类的内容。这听起来很糟糕,所以我想要一些建议,如何实现我的目标,即在没有共享/公共代码存在于父包中时,对一类相似的包进行分组。
英文:
I am building a web application in Go and as part of it I have several middelware functions defined. Right now they all live in "my/middleware" package. That namespace is becoming very cluttered by all the different functions I've defined so I decided to put them all in their own subdirectories, e.g. "my/middleware/gzip". When I do this I get the error:
no buildable Go source files my/middleware
I don't want all of these functions in the same namespace, but it seems my only option is to create a placeholder .go file in the my/middleware directory with an empty init function or something. That sounds terrible so I'd like suggestions on how to achieve my goal to group a similar class of packages when there isn't any shared/common code to live in the parent package.
答案1
得分: 0
你通过将文件拆分为不同的子文件夹来做出了正确的决策。这与这里所做的操作没有什么不同:
https://golang.org/pkg/compress/
这样可以让你的框架的客户端只获取他们需要的部分。这个想法是为了避免依赖膨胀。Go 一直以来都注重精简。
你收到的错误是因为你尝试构建一个不存在的包。把这个文件夹看作是一个逻辑分组机制,你需要单独构建子文件夹中给出的包。
英文:
You are actually taking the right decision by splitting the files into different subfolders. It is not different than what is done here
https://golang.org/pkg/compress/
This allows for the clients of your framework to take only what they need. The idea is to avoid dependency bloating. Go is all about being lean.
The error you receive is because you try to build a package that doesn't exist. Think of that folder as a logical grouping mechanism, you need to build the packages given by the child folders individually.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论