<p>I have a suspicion that the answer is "you're not supposed to do this", but I'm hoping otherwise. Everything I can find by searching and on StackOverflow is people talking about referencing other packages or creating libraries; that's not really what I'm after.</p>
<p>Let'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't separate packages, they'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's not is it something somebody has created a tool for? This is purely for aesthetics and I'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's not possible (and yes, you don'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'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's exactly the problem I'm stating. I don'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 'package main' - 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'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'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 "I want" 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't import</p></pre>
这是一个分享于 的资源,其中的信息可能已经有所发展或是发生改变。
入群交流(和以上内容无关):加入Go大咖交流群,或添加微信:liuxiaoyan-s 备注:入群;或加QQ群:692541889
- 请尽量让自己的回复能够对别人有帮助
- 支持 Markdown 格式, **粗体**、~~删除线~~、
`单行代码`
- 支持 @ 本站用户;支持表情(输入 : 提示),见 Emoji cheat sheet
- 图片支持拖拽、截图粘贴等方式上传