<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'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 "tree" 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'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's package mylib)</p>
<p>And in your main.go:</p>
<pre><code>package main
import (
"foo/mylib"
"fmt"
)
func main() { fmt.Println(mylib.tree) }
</code></pre></pre>PMaDinaTuttar: <pre><p>treeUp.go:6:2: cannot find package "foo/mylib" 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'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't there - then it obviously won'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 "package main" at the top, not something else?</li>
<li>In mylib/treeUp.go, you import "tree", but seem to expect that it can be found. Where does this "tree" come from? The import path should be absolute, like "myproject/tree", "mylib/tree", "github.com/user/project", etc.</li>
<li>Why is your GOPATH "go/test"? 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'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 "commands" 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 "main".</p>
<p>It'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's likely you have multiple packages in the one directory. You should also share what error messages you're seeing (it's otherwise very hard to help you).</p></pre>PMaDinaTuttar: <pre><p>Yes, but I have spent two hours and I still can't get it to work. I have a bunch of small projects to do in the coming months and I don'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 "package main" 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 "anotherPackage" or "myProject/anotherPackage" respectively. </p>
<p>Once you've fixed the organization of your code, just cd into $GOPATH/src/myProject and type "go build" and it'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'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 "go build"</p>
<p>Maybe helpful for you~</p>
<p>Btw, I don't like it.</p></pre>
这是一个分享于 的资源,其中的信息可能已经有所发展或是发生改变。
入群交流(和以上内容无关):加入Go大咖交流群,或添加微信:liuxiaoyan-s 备注:入群;或加QQ群:692541889
0 回复
- 请尽量让自己的回复能够对别人有帮助
- 支持 Markdown 格式, **粗体**、~~删除线~~、
`单行代码`
- 支持 @ 本站用户;支持表情(输入 : 提示),见 Emoji cheat sheet
- 图片支持拖拽、截图粘贴等方式上传