Weirdness of golang imports

xuanbao · · 741 次点击    
这是一个分享于 的资源,其中的信息可能已经有所发展或是发生改变。
<p>Hello,</p> <p>I&#39;ve been looking for a new language and go is perfect. Until you want to structure your code. I&#39;m a web developer and i always want my code to be MVC.</p> <p>So what did i do? I created a controllers folder and imported it into my main.go using import &#34;./controllers&#34;. It works until i want to use a &#34;github.com/...&#34; package in one of my controllers. I really don&#39;t understand this. It doesn&#39;t work and when i replace it with &#34;myproject/controllers&#34; it does work. But how absurd is that. That means if i change my folder name my entire code is broken.</p> <p>I&#39;ve read so many threads on this and all these people stating that relative imports are bad practice, i lolled. Relative imports are way more logical than absolute imports. Absolute = bad practice. It reminds me of wordpress storing absolute URLs in their database xD It&#39;s just so retarded.</p> <p>Can anyone give me a good reason why they do this. Or if possible a work-around so i can still use relative paths.</p> <p>Edit: I&#39;ve seen other people do it: <a href="http://idealogylabs.com/blog/golang/2015/07/11/golang-and-mvc.html" rel="nofollow">http://idealogylabs.com/blog/golang/2015/07/11/golang-and-mvc.html</a> Doesn&#39;t work for me (1.5.1)</p> <p>(Still no reasons given yet, i must be right.. i guesse.. Although, someone mentioned that it&#39;s just how it is and the language might not be what im looking for = fair point)</p> <hr/> <p>Someone (PsyWolf) linked me this: <a href="https://groups.google.com/forum/m/#!topic/golang-nuts/1XqcS8DuaNc/discussion" rel="nofollow">https://groups.google.com/forum/m/#!topic/golang-nuts/1XqcS8DuaNc/discussion</a></p> <p>And here you can read that relative paths work if your project is outside your GOPATH :o Perfect! it works! It&#39;s like almost no-one knows this or keeps it a secret. I&#39;m amazed! 100x Thank you for sharing that link.</p> <p>Or am i just stupid for placing my project in GOPATH/src?</p> <hr/>**评论:**<br/><br/>bradfitz: <pre><p>You might try asking the question without first claiming that &#34;It&#39;s just so retarded&#34;. You could say &#34;stupid&#34; instead if you really need to express an opinion, or perhaps you could ask a question and assume the authors made it that way based on experience or some design principle.</p></pre>cortexio: <pre><p>I&#39;m sure they had a reason for it. But i&#39;m also sure it&#39;s not a good one.</p></pre>jerf: <pre><p>Bending a language&#39;s design for how is modules work for one particular interpretation of one particular design pattern would be, what&#39;s the word, &#34;retarded&#34;.</p> <p>MVC merely mandates that you have classes that are clearly either M, V, or C. It does not require that you organize them in any particular way. That is a requirement you are bringing to the party, not MVC.</p> <p>Personally I&#39;ve never seen the point of that organization. I&#39;ve always preferred to organize them by logical units of functionality, not by a rather arbitrary structural distinction.</p></pre>cortexio: <pre><p>Hey look at my project, 1 folder with 100 files. xD</p> <p>Anyway. Relative paths do work. You just need to place your project outside the GOPATH/src , I didn&#39;t know this. Is that obvious? I might have followed the wrong tutorials.</p></pre>icholy: <pre><blockquote> <p>Hey look at my project, 1 folder with 100 files. xD</p> </blockquote> <p>&#34;I only know MVC, and everything else must suck.&#34;</p></pre>cortexio: <pre><p>Well, yeah... that&#39;s why everything is MVC in web unless you&#39;re making a tiny API. It&#39;s not about MVC though, it&#39;s more about your code being spread out in a structured way. Having everything in 1 folder reminds me of the 90s.</p></pre>PsyWolf: <pre><p>If you try to force your preconceived notions from other languages into go, you&#39;re gonna have a bad time. I&#39;m not just talking about this one case, but in general. Go is a language where if you want to use it effectively, you really have to give up control about these tiny things (Relative vs absolute imports, vendoring vs package management, spaces vs tabs, generics vs code generation, etc). If you must have your way about these things then go probably isn&#39;t the language for you.</p></pre>cortexio: <pre><p>Understandable. Still i&#39;d like to know why =/ I&#39;ve been checking almost every language and Go is the only one that gets close to being a good language. Node js is single core callback hell. Rust and elixir aren&#39;t web ready yet. The only decent languages are php, ruby and python. But they are slow compared to compiled languages. If only someone could make the perfect compiled language... Go is the closest thing to that.</p></pre>PsyWolf: <pre><p>And as for why go prefers absolute import paths, check out</p> <p><a href="https://groups.google.com/forum/m/#!topic/golang-nuts/1XqcS8DuaNc/discussion" rel="nofollow">https://groups.google.com/forum/m/#!topic/golang-nuts/1XqcS8DuaNc/discussion</a></p> <p>It was discussed at length there</p> <p>Edit: and also <a href="https://codereview.appspot.com/5787055" rel="nofollow">https://codereview.appspot.com/5787055</a></p></pre>cortexio: <pre><p>OMG! You solved my problem! It&#39;s only when you are working inside the GOPATH that you can&#39;t use relative paths. Outside your gopath they work perfectly! Thanks!</p></pre>PsyWolf: <pre><p>Sigh... I hope you realize that you&#39;re doing exactly the opposite of every piece of advice in this thread. I&#39;m asking you one last time to forsake this madness.</p> <p>By abandoning GOPATH, you&#39;re officially fighting the language. It&#39;s not that the community doesn&#39;t still want to help you, but we simply won&#39;t know how. You&#39;re going down a road that most of us have never traveled. If you do this, you&#39;re on your own. Good luck.</p></pre>PsyWolf: <pre><p>Then by all means, stick with go. Just try not to sweat small details like this. It&#39;s OK to think go&#39;s way of doing something is wrong. I have my own small gripes about the language too, but I&#39;m most effective with go when I ignore them and try doing things &#34;the go way&#34;. More often than not, the thing I thought was so awful, isn&#39;t all that bad. Sometimes it turns out to be even better than what I wanted go to let me do in the first place. <strong>cough</strong> error handling <strong>cough</strong></p></pre>devsquid: <pre><p>You could use the JVM if performance is a concern.</p></pre>Manbeardo: <pre><p>This topic is pretty well covered in several other locations. <a href="http://stackoverflow.com/questions/10687627/relative-import-from-parent-directory" rel="nofollow">Relevant StackOverflow</a>.</p></pre>zackkitzmiller: <pre><p>Relative imports actually are a bad practice. Perhaps you should try to listen to people before laughing at them. </p> <p>Storing an absolute URL in a datastore is quite a bit different than code structure. </p></pre>cortexio: <pre><p>But why is it bad practice? I mean, people might say &#34;Water is bad for you&#34;. You still need to know why else you might believe anything.</p></pre>zekzekus: <pre><p>it looks like this weirdness is a result of a design choice with go packages. </p> <p>as i can see nobody did mention any reasonable argument about this design. i can find only one sentence about this, something like &#34;a go package must be imported with an unique identifier everywhere&#34;.</p> <p>at the end, it looks weird and bad design. and yes, may be the language is not for you. you should read this: <a href="https://news.ycombinator.com/item?id=9485741" rel="nofollow">https://news.ycombinator.com/item?id=9485741</a></p> <p>i do not prefer to drive this golang car. may be you shouldn&#39;t either. </p></pre>cortexio: <pre><p>After finding out how it works, i can say: Go designed it perfectly. Only... It&#39;s like almost all go-devs don&#39;t know how it works. They all seem to jump to the retarded sentence &#34;It&#39;s bad practice&#34;. And it&#39;s not. Ofcourse it&#39;s not. But hey. Almost everyone said &#34;angular is awesome&#34;. = Majority of devs are shitty devs.</p> <p>So how it works:</p> <p>In GOPATH/src, use absolute paths</p> <p>Projects outside GOPATH/src, use absolute or relative paths based on your needs.</p></pre>calebdoxsey: <pre><p>If you don&#39;t follow the convention your project will not be go-gettable. It&#39;s bad practice because it doesn&#39;t play well with others.</p> <p>If you use goimports this is largely a non-issue. It will manage your imports so you rarely ever type it out. (And an editor like atom+go-plus or vim comes with all these bits included)</p> <p>Renaming a project is rare. But it&#39;s also not difficult as you can easily use a find and replace.</p> <p>In other words the design decision may seem strange, but in practice it has little impact on productivity. In fact its simplicity led to things like godoc.org.</p></pre>cortexio: <pre><p>That&#39;s the thing. It doesn&#39;t need to be go-gettable. I&#39;m working on a web project in go and not a re-use-able go package.</p></pre>xsolarwindx: <pre><p>Thanks for letting us know that it&#39;s retarded. I&#39;ll be sure to pass the message along to Rob Pike and Ken Thompson et. al. </p></pre>cortexio: <pre><p>if you could, that would be great actually.</p></pre>

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

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