Sorting files from the same package into folders

xuanbao · · 761 次点击    
这是一个分享于 的资源,其中的信息可能已经有所发展或是发生改变。
<p>I have a suspicion that the answer is &#34;you&#39;re not supposed to do this&#34;, but I&#39;m hoping otherwise. Everything I can find by searching and on StackOverflow is people talking about referencing other packages or creating libraries; that&#39;s not really what I&#39;m after.</p> <p>Let&#39;s say I have an app, and as part of that I have some configuration-type stuff (config.json, routes.go, database.go and so on) that I want to sort into subdirectories for neatness. Thus:</p> <pre><code>bin/ pkg/ src/ project/ main.go foo.go config/ config.json routes.go db/ database.go schema.sql </code></pre> <p>And so on and so forth. They aren&#39;t separate packages, they&#39;re not supposed to be imported anywhere, I just want <code>go build</code> to look in subdirectories of the project folder and find them as it would if they were all in one directory.</p> <p>Is this feasible by default? If it&#39;s not is it something somebody has created a tool for? This is purely for aesthetics and I&#39;d quite like it to be invisible magic; if I wanted to use multiple packages I could just use an import with the full path, and I do so where I have something I might need (or got from) elsewhere.</p> <hr/>**评论:**<br/><br/>TheMerovius: <pre><p>No, it&#39;s not possible (and yes, you don&#39;t want to do that).</p></pre>Xychologist: <pre><p>Fair enough. That was the conclusion I was coming to. Thanks anyway.</p></pre>robertmeta: <pre><p>Is this feasible by default?</p> <p>No.</p> <blockquote> <p>If it&#39;s not is it something somebody has created a tool for?</p> </blockquote> <p>No. It is confusing, ugly and goes against the nature of packages. It is valueless structural noise.</p> <hr/> <p>Go is very opinionated, if you try to fight it -- you will likely end up very unhappy. When in Goland, do as the Gophers do. </p></pre>blueblank: <pre><p>main-foo, routes, and database would all be seen as separate packages</p> <p>Packages are one level of files, its not that difficult and sometimes advisable to create subpackages in your main package.</p></pre>Xychologist: <pre><p>That&#39;s exactly the problem I&#39;m stating. I don&#39;t want them to be separate packages. They may reference files within each other in a way that would create circular or two-way dependencies, quite aside from anything else, and they would require importing. I want one single package that just happens to have some of its files located in subfolders. Database.go and routes.go both start with &#39;package main&#39; - that is, <code>go build</code> should look into the subdirectories, honour that instruction and not ask any questions or throw any errors.</p></pre>natefinch: <pre><p>You can&#39;t do that. directory == package, period. </p> <p>You can put the data files in a folder that starts with an underscore, and the go tool will ignore it.. but if you want two files to be in the same package they <em>must</em> be in the same directory.</p> <p>That&#39;s the way go works. You should embrace it. Doing it differently because you disagree with the way go works is just asking for heartache and pain.</p></pre>drvd: <pre><p>Technically it is doable. Like it is technically possible to kneel down on broken glass. Doable but insane: Invoke the compiler directly, manually, not through the go tool.</p></pre>drvd: <pre><blockquote> <p>that I want to sort into subdirectories</p> </blockquote> <p>There is no &#34;I want&#34; in Go.</p></pre>zeroZshadow: <pre><p>Hooray, another person that wants the same feature as I.</p></pre>JokerSp3: <pre><p>Try go internal package. It lets you have sub packages other people can&#39;t import</p></pre>

入群交流(和以上内容无关):加入Go大咖交流群,或添加微信:liuxiaoyan-s 备注:入群;或加QQ群:692541889

761 次点击  
加入收藏 微博
0 回复
暂无回复
添加一条新回复 (您需要 登录 后才能回复 没有账号 ?)
  • 请尽量让自己的回复能够对别人有帮助
  • 支持 Markdown 格式, **粗体**、~~删除线~~、`单行代码`
  • 支持 @ 本站用户;支持表情(输入 : 提示),见 Emoji cheat sheet
  • 图片支持拖拽、截图粘贴等方式上传