[beginner] How to import your own code

polaris · · 741 次点击    
这是一个分享于 的资源,其中的信息可能已经有所发展或是发生改变。
<p>For example, in the gitlab ci multirunner project there are import statements like (<a href="https://github.com/ayufan/gitlab-ci-multi-runner/blob/master/commands/multi.go#L17">example</a>)</p> <pre><code>import &#34;gitlab.com/gitlab-org/gitlab-ci-multi-runner/common&#34; </code></pre> <p>If I &#34;go get&#34; the project, I can build it. But when I fork it and put it under &#34;src/mystuff/gitlab-ci-multi-runner&#34;, I get build errors like</p> <pre><code>imports gitlab.com/gitlab-org/gitlab-ci-multi-runner/commands: unrecognized import path &#34;gitlab.com/gitlab-org/gitlab-ci-multi-runner/commands&#34; </code></pre> <p>If this is considered best practice in go, how to deal with this use case (fork)? If that&#39;s not the recommended way to use imports, what should I use for my own projects?</p> <p>Thanks.</p> <hr/>**评论:**<br/><br/>nstratos: <pre><p>I think using something like:</p> <pre><code>git remote add fork https://gitlab.com/you/gitlab-ci-multi-runner.git </code></pre> <p>on the original project code might solve your problem. Check out this <a href="http://blog.campoy.cat/2014/03/github-and-go-forking-pull-requests-and.html" rel="nofollow">post</a> of Francesc Campoy explaining the procedure.</p></pre>cptcomic: <pre><p>Interessting. Thanks. If would follow this post and make changes in &#34;server.go&#34;, and someone would &#34;go get github.com/you/cooltool.git&#34; wouldn&#39;t they also &#34;go get&#34; the original version and use &#34;server.go&#34; from this version?</p></pre>nstratos: <pre><p>You work directly on the original version. Your fork only exists on github. When you finish editing server.go then instead of pushing to the original, you push to your fork with: </p> <pre><code>git push fork </code></pre> <p>Your fork will then have the new version of server.go and finally you can make a pull request to the original repository to accept your new server.go from your forked repository.</p></pre>izuriel: <pre><p>This solution works fine if you&#39;re working on bug fixing or feature improvements to a repository. This wasn&#39;t mentioned in the question here so I didn&#39;t reply with any help as to solving this problem but explained why the issue questioned was occurring and it&#39;s resolution.</p> <p>Again, this is a great way to go about working on another project but doesn&#39;t solve the issue of &#34;I&#39;ve forked it to be significantly different or to be my own iterpretation&#34; (in which case import paths will have to change).</p></pre>izuriel: <pre><p>You changed the path of the source. You will need to rewrite those imports to the new path. The key here is that the error is informing you the path your trying to import (original) doesn&#39;t exist (because you moved the code to a different path)</p></pre>cptcomic: <pre><p>Thank you. I understand the problem. New to go, I just find it hard to believe, that hardcoding this kind of information into the source code, is the way to &#34;go&#34;.</p></pre>xrstf: <pre><p>It is the way to go. Relative imports don&#39;t work as you would expect, so we&#39;re stuck with this. If it bothers you, you are out of luck: If it&#39;s no problem for Google internally, they don&#39;t give a flying sh_tf_ck about it. Just look at the package manager debate where all Google is saying is &#34;we don&#39;t need multiple gopaths/versions in our world, so f* you!&#34;</p> <p>Sorry for being so negative, but Google&#39;s attitude is one of my main pain points with Go.</p></pre>izuriel: <pre><p>Google pays the developers of Go to build a language to meet the needs of Certain Google development teams so it would seem perfectly logical that an internal tool is solely based an their needs. Then they have this tool away to be used by other people but O fail to see where that would really require them to bend to the communities will, they could very well have kept this locked away behind closed doors at Google and never released it.</p> <p>If they don&#39;t need, it probably won&#39;t be added. It&#39;s the benefit of owning the software. If it bothers that much you can seek out other languages as options for your projects - or if you work on Go you would have a harder time getting away from it. </p> <p>Berating Google does nothing productive and paints you in a negative light. I don&#39;t like some things about Go, but that&#39;s what makes Go Go and not something else so I suck it up and move on with my life. </p></pre>xrstf: <pre><p>I do seek out other languages, but at the same time I can see Go&#39;s strong points. I didn&#39;t say that Go was an entirely bad language! Strong Windows support, minimal syntax, goroutines and super easy deployments are things I really enjoy in Go and are the reasons why I still use it for my projects. For situations where I need smaller binaries, I&#39;m looking at Nim (now that 1.0.0 is released). For web stuff, PHP is still my main choice.</p> <p>The things I don&#39;t like about Go are things like the ones I mentioned. I can&#39;t fork a Go package without also telling my users &#34;oh yeah, you need to go-get it and then move it to a different spot&#34; (or maybe I&#39;m overlooking cleverness in go get and it actually looks at the source to determine the target download directory). I can&#39;t keep a specific version of some dependency without using 3rd party tools or resorting to &#34;git commit all the things!&#34;.</p> <p>Maybe I expressed my concern badly in the previous post. What I wanted to say is that I can feel Google&#39;s strong influence on the language and its ecosystem, and not all of Google&#39;s choices are necessarily ones that are useful for the &#34;general public&#34;. If you choose Go, you are at the mercy of Google. And in the last years, that has proven problematic for me.</p> <p>(Rust, for comparision, feels much more community-influenced, even though it&#39;s steered by Mozilla. Not saying that Rust is better or worse than Go.)</p></pre>izuriel: <pre><blockquote> <p>Rust, for comparision, feels much more community-influenced, even though it&#39;s steered by Mozilla. Not saying that Rust is better or worse than Go.</p> </blockquote> <p>Rust does have a significant amount of community involvement, the difference in my eyes (as I have seen and read) is that Mozilla opened up Rust to the public and then significant amounts of community contributions have been made, including developing or determining language behaviors and features.</p> <blockquote> <p>Maybe I expressed my concern badly in the previous post.</p> </blockquote> <p>It sounded rather hostile to me, you have my apologies if that was incorrect assumption and my response was out of line.</p> <blockquote> <p>Nim (now that 1.0.0 is released)</p> </blockquote> <p>I didn&#39;t follow it much because I assumed this was further away, this is good to hear and I&#39;ll be playing with it again.</p> <blockquote> <p>If you choose Go, you are at the mercy of Google.</p> </blockquote> <p>Such is the world in which live. Although, IMHO, it&#39;s the same ecosystem with Java and Oracle. While recent developments have helped to bring very necessary (this is all IMHO) features to the language to make it an attractive option in the current landscape they still don&#39;t provide a native package manager with the language. C# is very similar, although generally viewed outside of these problems as most C# developers I&#39;ve dealt with in the past wear rather large rose-colored glasses (do not read into this that I hate C#, that&#39;s not what I&#39;m talking about) when it comes to that language and it&#39;s platform. Although Microsoft has begun pushing really hard into a very needed area for them, I&#39;m sure Google will work on implementing things as they come around.</p> <p>Package management has been discussed and the last thing I read from the Go team (which was actually a while back, so it&#39;s possible it&#39;s no longer true) there were talks about vendoring capabilities coming packaged in the go toolchain come Go 1.5 release.</p> <blockquote> <p>I can&#39;t fork a Go package without also telling my users &#34;oh yeah, you need to go-get it and then move it to a different spot&#34; (or maybe I&#39;m overlooking cleverness in go get and it actually looks at the source to determine the target download directory).</p> </blockquote> <p>Not sure I understand. The only requirement thing that has to change are import paths internal to the project itself. If you fork &#34;github.com/someuser/badass-project&#34; then all imports of that nature must change, the should change to the new path which would be &#34;github.com/awesome_user/badass-project&#34; instead. To your consumers, nothing changes. They can still &#34;go get github.com/awesome_user/badass-project&#34; and the go toolchain creates the necessary directory structure (in this case, it would be $GOPATH/src/github.com/awesome_user/badass-project) and any imports that specify that path pull in your fork. You can have this installed alongside the original repository as well because the paths are unique to each project and therefore you can import both versions simultaneously (although package aliases would be needed).</p> <p>If you clone projects, just path them the same way. If you want clone &#34;github.com/someuser/badass-project&#34; then do these simple steps:</p> <pre><code>$ mkdir -p $GOPATH/src/github.com/someuser $ cd $GOPATH/src/github.com/someuser $ git clone https://github.com/someuser/badass-project </code></pre> <p>And boom, done, import away like nothing every happened.</p> <p>P.S. go get also fetches from a large number of other remote source control options.</p> <p><strong>tl;dr</strong> I don&#39;t care if you&#39;re lazy. Read the damn thing.</p></pre>xrstf: <pre><blockquote> <p>Not sure I understand. The only requirement thing that has to change are import paths internal to the project itself. If you fork &#34;github.com/someuser/badass-project&#34; then all imports of that nature must change, the should change to the new path which would be &#34;github.com/awesome_user/badass-project&#34; instead. To your consumers, nothing changes.</p> </blockquote> <p>I have stayed away from package managers for Go because it seems the community has not yet settled on one (I hope that happens, it did <em>wonders</em> to the PHP world). That&#39;s why I haven&#39;t used go get extensively, so sorry for my lack of RTFM.</p> <p>How should I handle this scenario: I have some upstream package (<code>github.com/upstream/lib</code>). I have some patches and so I forked the library to <code>github.com/me/lib</code>, patched it and created PRs. Now I&#39;m waiting for them to be merged. What am I supposed to do in the meantime? It looks like I either have to</p> <ul> <li>document that you have to <code>go get github.com/me/lib</code> (and <em>not</em> use the upstream one, because that one has bugs) and that you have to place it into <code>$GOPATH/src/github.com/upstream/lib</code>).</li> <li>document that after getting <code>upstream/lib</code>, you need to add my fork as a remote and pull from that as well.</li> <li>commit the library to my project as well, with all the issues that brings.</li> </ul> <p>You are absolutely correct, forking with the intent of permanently diverging from the original project is no issue, because the paths/imports change accordingly.</p> <p>This all looks to me as a drawback Go inherits from directly linking the code origin (URL) with the package name. Sure it&#39;s easy and works for most situations (it hasn&#39;t bitten me yet in Go, but something like this would have caused problems many times with my PHP projects), but at the same time it <em>prevents</em> some more advanced approaches to handling packages. But then....</p> <blockquote> <p>Package management has been discussed and the last thing I read from the Go team (which was actually a while back, so it&#39;s possible it&#39;s no longer true) there were talks about vendoring capabilities coming packaged in the go toolchain come Go 1.5 release.</p> </blockquote> <p>This excites me indeed :)</p></pre>izuriel: <pre><p>To the scenario given, unfortunately, there are no elegant ways to handle that. This is, as you have mentioned, an unfortunate side effect of the way Go handles things (remember, I&#39;m not here proclaiming Go is almighty and the Go way is correct because it&#39;s Go). That being said though, it does have some relatively simple solutions like the ones you mentioned, although this falls onto the library consumer to accomplish those steps. We&#39;ll see if something comes out of.</p> <p>I can definitely understand your hesitation to pick a package manager, I myself have used Gom and Godep for projects for experimentation. Having a ruby background I definitely prefer Gom (Gomfile ~= Gemfile) although I found Godep was relatively simple. Unless Go itself pulls away from the workspace concept (which I doubt it will) I stay away from tools like wgo and gb (I&#39;m not saying anything negative about either project) but they both have their own takes on how a project should be structured. If either of those options find a home as the &#34;official&#34; way, I won&#39;t have issues with them - but in the flux I use what&#39;s comfortable and easy for me and the guys I work with.</p></pre>izuriel: <pre><p>It is only slightly different than other languages with import paths. It&#39;s not like anything has been <em>hardcoded</em> at all. It&#39;s an import path that expects that project to be set up the way it was written. The workspace is a key feature that allowed for simplification of tools. </p> <p>Making things truly simple usually means that certain features must be done away with. Here, importing is dead simple and the only sacrifice made was expected locations of those files. </p></pre>cptcomic: <pre><p>I&#39;m not arguing, just want to learn the go way - I&#39;m sure there are advantages I have yet to discover. </p> <p>Still, opening a bunch of files and changing the same string, is, what I call, hardcoded. </p></pre>izuriel: <pre><p>You should invest in a tool that automates that for you. </p></pre>cptcomic: <pre><p>Renaming things once is easy. In general, dealing with change (e.g. merging upstream), is what makes hardcoded stuff difficult to handle.</p> <p>It&#39;s something every programmer tries to avoid. That&#39;s why I was asking, if there is some other way. Thank you, for your explanations.</p></pre>izuriel: <pre><p>Sorry if I came off as hostile at any point, it was not my intention.</p></pre>cptcomic: <pre><p>I didn&#39;t perceive it as hostile at all. I really appreciate your help.</p></pre>

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

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