Golang子目录

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

Golang subdirectories

问题

有没有一种方法可以为我的一些文件创建一个子目录?这纯粹是为了文件组织。我有很多小的结构体/方法,我想把它们放到自己的文件和子目录中,但我不想把它们放到自己的包中。它们依赖于我项目中的其他功能。每个文件大约有10-50行,我一直把它们放在一个文件中,但这很丑陋。

英文:

Is there not a way to create a sub-directory for some of my files? This is purely for file organization. I've got a large number of small structs/methods that I'd like to put into their own files and into a sub-directory, but I don't want to put them into their own package. They rely on other functionality in my project. Each one is anywhere from 10-50 lines and I've been sticking them in one file, but this is ugly.

答案1

得分: 11

这是没有办法做到的。Go语言有意这样设计,因为如果你觉得需要另一个子目录,很可能你需要另一个包。另外,如果你真的不需要一个子包,那么你可以将所有这些小结构体及其相应的方法拆分到同一个包的不同文件中。

英文:

There is no way to do it. Go does this on purpose, because if you feel like you need another sub-directory. More than likely you need another package. Also, if you truly do not need a sub-package, then you could just split all those small structs and their appropriate methods into their own separate files in the same package.

答案2

得分: 0

你也可以将它们放在一个单独的包中,然后在父包中导入所有的内容:

package mainpackage

import . "mysubpackage" // 包含所有小的子目录
英文:

you could also put them in a separate package and them import all of the in the parent package:

  package mainpackage

   import . "mysubpackage" // with all the little sub directory 

huangapple
  • 本文由 发表于 2014年7月16日 00:22:51
  • 转载请务必保留本文链接:https://go.coder-hub.com/24763347.html
匿名

发表评论

匿名网友

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

确定