can't figure out os.Args

blov · · 1064 次点击    
这是一个分享于 的资源,其中的信息可能已经有所发展或是发生改变。
<p>I&#39;m trying to get the combined length of the arguments returned from the command line with this code</p> <pre><code>package main import ( &#34;fmt&#34; &#34;os&#34; ) func total(args []string) int { var bic = 0 for i := (len(args) - 1); i &gt; 0; i-- { bic = bic + len(args[i]) } return bic } func main() { fmt.Println(total(os.Args)) } </code></pre> <p>Can anybody enlighten me as to what&#39;s wrong?</p> <p>EDIT: Every time i run it, it returns 0. no idea why. EDIT2: I compiled it, and have been running from my path, only then does it return 0 every time. if i run </p> <pre><code>go run countArgs.go count this </code></pre> <p>it returns 9. What the heck?!</p> <p>EDIT3: I accidentally a thing</p> <p>EDIT4: I got it to work. Thanks!</p> <p>Thanks!</p> <hr/>**评论:**<br/><br/>schoenobates: <pre><p>It&#39;s working for me :</p> <pre><code>$ ./main a bcd ef $ 6 </code></pre> <p>Is that not what you were expecting?</p></pre>parrotjay: <pre><p>no matter what i do, it always returns 0. no idea why</p></pre>djhworld: <pre><p>Works fine for me </p> <pre><code>21:21:54 ~|⇒ go run args.go hello go 7 </code></pre></pre>parrotjay: <pre><p>no matter what i do, it always returns 0. dunno why the same code would work for two other people...</p></pre>alexwhoizzle: <pre><p>What version of Go? OS? </p></pre>parrotjay: <pre><p>mac, and the most recent version of go</p></pre>alexwhoizzle: <pre><p>Strange. Just ran it on my Mac w/ Yosemite and Go 1.4.1 and got the correct output. Also tried it on my Windows 8.1 machine w/ Go 1.4 and still got the correct result. Not sure what is happening in your case :\</p></pre>schoenobates: <pre><p>If it returns 8, was it a fat finger on the keyboard? e.g. hit the delete key and enter at the same time???</p></pre>Morningrise89: <pre><p>You are counting the number of bytes in each argument. To count letters in a string, you must count the number of runes in the string</p></pre>parrotjay: <pre><p>thanks! I&#39;m still working on understanding the whole rune thing... I&#39;m new to programming in general, so I figured jumping into a harder lang than i&#39;m used to would be a good project. How would you impliment that?</p></pre>Morningrise89: <pre><p>I misunderstood. If you run the program, it will have 1 Arguments. The program name itself will be args[0], but your loop goes for i &gt; 0. So not including argument 0. </p> <p>Now, &#39;go run&#39; are arguments to the go process. Not Your program.</p> <p>My phone and I apologize for random capitalization.</p> <p>Rune is go lang way of dealing with utf strings while still being friendly to Bytes. See the rune package for help.</p></pre>YEPHENAS: <pre><p>Runes aren&#39;t letters either.</p></pre>Morningrise89: <pre><p>Ok, you are right</p></pre>Saucysauce: <pre><p><a href="https://play.golang.org/p/h7zv1XVxy_" rel="nofollow">https://play.golang.org/p/h7zv1XVxy_</a></p> <p>Maybe try to get a runtime not installed on your system to do this?</p></pre>rickt: <pre><p>i copied your code to foo.go. then:</p> <pre><code>$ go build foo.go $ ./foo foo bar baz 9 $ go run foo.go foo bar baz 9 </code></pre> <p>when you say &#34;no matter what i do, it always returns 0&#34;, where are you running the executable from?</p> <p>if you&#39;re not doing ./foo (assuming your newly compiled executable is called foo), and &#34;.&#34; isn&#39;t in your $PATH, then you&#39;re running another executable called &#34;foo&#34; that is somewere else in your $PATH. </p> <p>if you compile &amp; run your executable using &#34;go run foo &lt;arguments&gt;&#34; that is the functional equivalent of doing:</p> <pre><code>$ go build foo.go &amp;&amp; ./foo </code></pre> <p>check your $PATH. </p></pre>parrotjay: <pre><p>I compiled it into my $GOPATH/bin (with go install) and have been running it from path. that is when i get the return of 0. I have even cd&#39;d into $GOPATH/bin and run it directly from that folder, to no avail. </p> <p>Just now I tried with go build and running with ./foo, and that worked alright, same as go run foo.go, but still when I try and run the code from path it just doesn&#39;t work. what in the heck?!</p> <p>also, how do i check my path for executables of the same name?</p> <p>If I change the contents of foo.go to </p> <pre><code>func main() { fmt.Println(&#34;FOO!&#34;) } </code></pre> <p>run go install, and try and run it, it returns &#34;FOO!&#34; as expected. </p></pre>rickt: <pre><blockquote> <p>I have even cd&#39;d into $GOPATH/bin and run it directly from that folder, to no avail.</p> </blockquote> <p>even if you have cd&#39;d into $GOPATH/bin, unless you are running it in that folder using ./foo you will be running &#34;foo&#34; from somewhere else in your path -- unless . is in your $PATH. which it clearly isn&#39;t. EDIT: &#34;.&#34; could be in your $PATH, but if it is, it&#39;s later in the variable than the folder that has the other executable named &#34;foo&#34;. </p> <p>the problem you&#39;re having is not Go-related. it&#39;s shell-related.</p> <p>remove/rename the binary that is causing the problem. depending on your shell, you can find which one it is by running &#34;whereis foo&#34; or &#34;which foo&#34;, obviously substituting &#34;foo&#34; for your real binary name.</p></pre>parrotjay: <pre><p>got it to work. Thanks!</p></pre>

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

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