Is there a simple way of compiling golang?

xuanbao · · 757 次点击    
这是一个分享于 的资源,其中的信息可能已经有所发展或是发生改变。
<p>I have been stuck trying to compile a golang project for an hour now and even though I really like golang the compilation process is extremely annoying with piles of new folders and dozens of commands trying to set it all up and still it can&#39;t find my files. </p> <p>Is there an alternative compiler or method of compiling a golang project that lets me do this:</p> <p>~/myproject/</p> <p>myfile.go</p> <p>anotherfile.go</p> <p>containsMain.go</p> <pre><code>go build containsMain.go </code></pre> <hr/>**评论:**<br/><br/>Fwippy: <pre><p>Have you tried <code>go build</code>?</p></pre>PMaDinaTuttar: <pre><p>Yes and it works well with one file. The problem is when I have multiple files. </p> <p>Currently I have:</p> <p>~/myProj /bin/ /pkg/ /src/foo/mylib/afile.go /src/foo/quux/containsMain.go</p> <p>afile compiles just fine and I get the pkg. containsMain just gives</p> <blockquote> <p>foo/mylib/treeUp.go:6:2: cannot find package &#34;tree&#34; in any of: /usr/local/go/src/tree (from $GOROOT) /Users/qwerty/Documents/arbetsmapp/go/test/src/tree (from $GOPATH)</p> </blockquote></pre>nerdwaller: <pre><p>Seems like your GOPATH is improperly configured, or more likely from the error: you aren&#39;t importing absolute paths from the GOPATH.</p> <p>Should be fine with:</p> <p>GOPATH=~/go Main=~/go/src/foo/quux/main.go Lib=~/go/src/foo/mylib/afile.go (I am guessing it&#39;s package mylib)</p> <p>And in your main.go:</p> <pre><code>package main import ( &#34;foo/mylib&#34; &#34;fmt&#34; ) func main() { fmt.Println(mylib.tree) } </code></pre></pre>PMaDinaTuttar: <pre><p>treeUp.go:6:2: cannot find package &#34;foo/mylib&#34; in any of: /usr/local/go/src/foo/mylib (from $GOROOT) /Users/katleb/Documents/arbetsmapp/go/src/foo/mylib (from $GOPATH)</p></pre>nerdwaller: <pre><p>Your previous comment suggests that your project is at <code>~/myproj</code> with <code>{bin,src,pkg}</code> under it? Your $GOPATH (from this output) is <code>~/Documents/arbetsmapp/go</code> (e.g. your project isn&#39;t in your $GOPATH).</p> <p>Just check your locations again, the debug statements tell you where it is trying to look, if your package isn&#39;t there - then it obviously won&#39;t find it. If you move it there, it will start working - assuming all else is set up correctly.</p></pre>cube2222: <pre><p>Show us your code.</p></pre>joushou: <pre><p>Okay, we need some source, and your full folder structure.</p> <ol> <li>Do all your files in myproject/ have &#34;package main&#34; at the top, not something else?</li> <li>In mylib/treeUp.go, you import &#34;tree&#34;, but seem to expect that it can be found. Where does this &#34;tree&#34; come from? The import path should be absolute, like &#34;myproject/tree&#34;, &#34;mylib/tree&#34;, &#34;github.com/user/project&#34;, etc.</li> <li>Why is your GOPATH &#34;go/test&#34;? Are you using some GOPATH management tool?</li> </ol> <p>We need some source to tell you what is wrong. Building Go is incredibly simple, but I suspect your imports are pointing into nothingsness, and you might not have fully understood the concept of GOPATH.</p> <p>A side-note: If you want to have a project with a main package (containsMain.go, I believe) and a lib, you should organize it so that the main package is in a separate folder. You can either put the lib in myproject, and the main package in myproject/cmd, or put the lib in myproject/lib and the main package in myproject, depending on taste.</p></pre>WellAdjustedOutlaw: <pre><p>I suspect you&#39;re doing something seriously wrong. Have you set your GOPATH environment variable?</p></pre>CaffeineComa: <pre><p>I suspect the problem is that (s)he likely has a main package in containsMain.go, and some other package in myfile.go and anotherfile.go. </p> <p>Recommendation: create a &#34;commands&#34; subdir, move containsMain.go in there, and import your package(s) in that file. Or else leave the files in place, but edit them all to be in package &#34;main&#34;.</p> <p>It&#39;s meant to be simple in Go, but honestly this gave me a headache in the beginning too.</p></pre>elithrar_: <pre><p>Have you read <a href="https://golang.org/doc/code.html#Organization" rel="nofollow">https://golang.org/doc/code.html#Organization</a> ? As others have assumed, it&#39;s likely you have multiple packages in the one directory. You should also share what error messages you&#39;re seeing (it&#39;s otherwise very hard to help you).</p></pre>PMaDinaTuttar: <pre><p>Yes, but I have spent two hours and I still can&#39;t get it to work. I have a bunch of small projects to do in the coming months and I don&#39;t want multiple folders for each project. </p> <p>Is there anyway to compile it with multiple files in one folder? I am used to compiling c with gcc. Is there no way of doing something similar with golang?</p></pre>elithrar_: <pre><p>Yes, but those multiple files need to be part of the same package.</p> <p>To re-state: <em>what error message are you seeing</em>?</p></pre>hey_bros_its_gerry: <pre><p>You should have the folder $GOPATH/src/myProj</p> <p>That folder can contain unlimited files, but all files that end in .go must have the &#34;package main&#34; declaration at the top. If you want additional packages, create a new folder $GOPATH/src/anotherPackage or a subpackage like $GOPATH/src/myProject/anotherPackage and import it with &#34;anotherPackage&#34; or &#34;myProject/anotherPackage&#34; respectively. </p> <p>Once you&#39;ve fixed the organization of your code, just cd into $GOPATH/src/myProject and type &#34;go build&#34; and it&#39;ll spit out a binary if you have a func main() </p></pre>JokerSp3: <pre><p>go build *.go will compile all go files in the dir together</p></pre>MrSaints: <pre><p>Build it using Docker (<a href="https://github.com/arachnys/athenapdf/blob/master/weaver/Dockerfile.build" rel="nofollow">example</a>). There&#39;s also a pretty straightforward <a href="https://medium.com/iron-io-blog/the-easiest-way-to-develop-with-go-introducing-a-docker-based-go-tool-c456238507d6" rel="nofollow">guide</a> on it. Also, you can easily mount your current working directory to the build folder or run the binary in a Docker container.</p></pre>yo2me: <pre><p>try like nsq, for more detail you can read the Makefile of nsq. <a href="https://github.com/nsqio/nsq" rel="nofollow">https://github.com/nsqio/nsq</a></p> <p>The Nsq copy *.go file to the apps/nsq/ and run the command &#34;go build&#34;</p> <p>Maybe helpful for you~</p> <p>Btw, I don&#39;t like it.</p></pre>

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

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