<p>I'm trying to get the combined length of the arguments returned from the command line with this code</p>
<pre><code>package main
import (
"fmt"
"os"
)
func total(args []string) int {
var bic = 0
for i := (len(args) - 1); i > 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'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'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'm still working on understanding the whole rune thing... I'm new to programming in general, so I figured jumping into a harder lang than i'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 > 0. So not including argument 0. </p>
<p>Now, 'go run' 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'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 "no matter what i do, it always returns 0", where are you running the executable from?</p>
<p>if you're not doing ./foo (assuming your newly compiled executable is called foo), and "." isn't in your $PATH, then you're running another executable called "foo" that is somewere else in your $PATH. </p>
<p>if you compile & run your executable using "go run foo <arguments>" that is the functional equivalent of doing:</p>
<pre><code>$ go build foo.go && ./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'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'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("FOO!")
}
</code></pre>
<p>run go install, and try and run it, it returns "FOO!" as expected. </p></pre>rickt: <pre><blockquote>
<p>I have even cd'd into $GOPATH/bin and run it directly from that folder, to no avail.</p>
</blockquote>
<p>even if you have cd'd into $GOPATH/bin, unless you are running it in that folder using ./foo you will be running "foo" from somewhere else in your path -- unless . is in your $PATH. which it clearly isn't. EDIT: "." could be in your $PATH, but if it is, it's later in the variable than the folder that has the other executable named "foo". </p>
<p>the problem you're having is not Go-related. it'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 "whereis foo" or "which foo", obviously substituting "foo" for your real binary name.</p></pre>parrotjay: <pre><p>got it to work. Thanks!</p></pre>
这是一个分享于 的资源,其中的信息可能已经有所发展或是发生改变。
入群交流(和以上内容无关):加入Go大咖交流群,或添加微信:liuxiaoyan-s 备注:入群;或加QQ群:692541889
- 请尽量让自己的回复能够对别人有帮助
- 支持 Markdown 格式, **粗体**、~~删除线~~、
`单行代码`
- 支持 @ 本站用户;支持表情(输入 : 提示),见 Emoji cheat sheet
- 图片支持拖拽、截图粘贴等方式上传