<p>I've gotten myself twisted around about this. I spent a week learning Go, then had to walk away for a week, and now when I came back, I can't 'install' as I did before.</p>
<p>The code compiles but won't install and I get the error: </p>
<blockquote>
<p>go install: no install location for .go files listed on command line (GOBIN not set)</p>
</blockquote>
<p>In my .profile I have set:</p>
<blockquote>
<p>export GOPATH=/home/me/go</p>
</blockquote>
<p>My little test program is straight from the Go docs:</p>
<pre><code>package main
import "fmt"
func main() {
fmt.Printf("Hello, world.\n")
}
</code></pre>
<p>"go run test.go" runs the program. "go install test.go" gives the error I showed before.</p>
<p>What am I missing?</p>
<hr/>**评论:**<br/><br/>webRat: <pre><p>Some things you need to know:</p>
<pre><code>/home/me/go/src/ is your source directory
/home/me/go/bin/ is your bin directory
</code></pre>
<p>When you run <code>go install</code> (compile and install packages and dependencies), It's compiling and putting the compiled file in the bin directory.</p>
<p>Your project setup should be:
<code>/home/me/go/src/helloworld/main.go</code></p>
<p>In that directory, you would type <code>go build</code> nothing more. No filenames, no nothing. It'll create a helloworld binary in that directory that you're in. When you're ready, then you can type <code>go install</code> and it'll put it in the bin directory.</p></pre>goboy234: <pre><p>My set up is as you describe but the docs on golang say nothing about doing a build first in their "How to Write Go Code" page. I do the "install" just as they describe.</p></pre>jszwedko: <pre><p><code>install</code>will build it, but <code>go install</code> takes a package as an argument, not files (the default is the package in the current directory).</p></pre>jszwedko: <pre><p>See <a href="https://golang.org/cmd/go/" rel="nofollow">https://golang.org/cmd/go/</a> for documentation (or just run <code>go install -h</code>).</p></pre>goboy234: <pre><p>That is my understanding but it conflicts with the docs unless somehow I'm misreading them on the page I talked about. I can build, then install, but the docs say just to install. </p>
<p>In any case, I'm able to make this work. Thanks.</p></pre>drvd: <pre><p>The go tool normally works on a <strong>package</strong> <strong>level</strong>, not on a file level.</p>
<p>You do a</p>
<pre><code>go install
</code></pre>
<p>or a </p>
<pre><code>go build
</code></pre>
<p>for whole packages only, typically the package contained in the current folder.</p>
<p>You either do a</p>
<pre><code>go install
</code></pre>
<p>to install the package in the current directory or you do a</p>
<pre><code>go install full/path/to/package
</code></pre>
<p>to instal package the package contained in $GOROOT/src/full/path/to/package</p>
<p>The only exception to the "use the go tool on packages only" is for</p>
<pre><code>go run
</code></pre>
<p>which operates on a file level.</p>
<p>So your </p>
<pre><code>go install test.go
</code></pre>
<p>is completely wrong as there are (and cannot be) packages named "test.go". A simple</p>
<pre><code>go install
</code></pre>
<p>is all you need.</p></pre>iku_19: <pre><p>the command is <code>go build</code> i believe</p>
<p>(From what I remember)</p>
<p>If you want to compile a <code>package main</code> project to binary, <code>go build [files]</code></p>
<p>If you want to compile a <code>package main</code> project to binary and install it, export <code>GOBIN</code> and <code>go install [files]</code></p>
<p>If you want to link a <code>package non-main</code> project to be used in other projects, export <code>GOPATH</code> and <code>go install</code></p></pre>
这是一个分享于 的资源,其中的信息可能已经有所发展或是发生改变。
入群交流(和以上内容无关):加入Go大咖交流群,或添加微信:liuxiaoyan-s 备注:入群;或加QQ群:692541889
- 请尽量让自己的回复能够对别人有帮助
- 支持 Markdown 格式, **粗体**、~~删除线~~、
`单行代码`
- 支持 @ 本站用户;支持表情(输入 : 提示),见 Emoji cheat sheet
- 图片支持拖拽、截图粘贴等方式上传