GO code runs but won't install

blov · · 708 次点击    
这是一个分享于 的资源,其中的信息可能已经有所发展或是发生改变。
<p>I&#39;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&#39;t &#39;install&#39; as I did before.</p> <p>The code compiles but won&#39;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 &#34;fmt&#34; func main() { fmt.Printf(&#34;Hello, world.\n&#34;) } </code></pre> <p>&#34;go run test.go&#34; runs the program. &#34;go install test.go&#34; 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&#39;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&#39;ll create a helloworld binary in that directory that you&#39;re in. When you&#39;re ready, then you can type <code>go install</code> and it&#39;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 &#34;How to Write Go Code&#34; page. I do the &#34;install&#34; 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&#39;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&#39;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 &#34;use the go tool on packages only&#34; 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 &#34;test.go&#34;. 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

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