<p>Excuse the noob question, but I am only now getting started with the language. Any help is appreciated, since I haven't been able to find an answer online.</p>
<p>I have written a function that on some input does some calculations and returns a string. Then, I am testing (with Go's test suite) whether that string is anything other than a given string (with the '!=' operator). This is always true (meaning the strings are always different) and an error is raised.</p>
<p>In pseudocode, to understand what I'm talking about:</p>
<p><a href="https://pastebin.com/aXPGurTz" rel="nofollow">https://pastebin.com/aXPGurTz</a></p>
<p>The problem is if I print the output of the function (f), it actually <em>is</em> the same as the given string (at least it prints the same).</p>
<p>Do you have any idea why this is happening? Maybe there are different encodings of strings and something gets messed up?</p>
<p>EDIT: (Skip to EDIT2) <del>Thank you all for the interest, but it seems I've shot myself in the foot here. I have been working on this project for about 3-4 days, incrementally experimenting with more complex stuff, and unfortunately this was literally the last thing I was about to do. That means the project has quite a lot of moving parts (from data reading, to heaps) and a lot of code. The best I could do is provide a zip file with the contents. You can extract and then type "go test" to see the issue. The download link is: <a href="http://www.mediafire.com/file/w4zzi8c8ca6jnej/hey.7z" rel="nofollow">http://www.mediafire.com/file/w4zzi8c8ca6jnej/hey.7z</a></del></p>
<p>Another thing I found, the returned "thing" doesn't act like a string. Take a look at this (this is how it's supposed to work): <a href="https://play.golang.org/p/I6i-kscVx8" rel="nofollow">https://play.golang.org/p/I6i-kscVx8</a></p>
<p>In my case, it wouldn't return "aaB" but "Ba" (for a := MyFunction()). Maybe this helps?</p>
<p>PS: I know the file directory doesn't follow the Go standards, sorry about that. I was just hacking away to see if Go was to my liking, and this was the simplest way to do it.</p>
<p>EDIT2: Jerf used his psychic skills to pinpoint the problem (it was a rogue '\r' at the end of the string). Thanks a lot Jerf, and thanks everyone for helping out!</p>
<hr/>**评论:**<br/><br/>jerf: <pre><p>Use fmt.Sprintf to print the two strings using the %q format. My psychic powers suggest one string has a newline on the end and the other does not.</p></pre>throwie_confused: <pre><p>Your psychic powers are working great mate. There was a rogue '\r' at the end of the returned string.</p>
<p>Thanks a lot!</p></pre>slewiskelly: <pre><p>You can use strings.TrimSpace() to remove all leading and trailing whitespace characters from your strings.</p>
<p>If your strings have leading or trailing whitespace and only want to remove line feeds (\n) and carriage returns (\r), read up on strings.TrimFunc().</p></pre>mcandre: <pre><p>There are many runes which appear the same but to a computer are rather distinct at a bit level. See the Ruby fiasco with allowing UTF-8 invisible characters as variable names.</p>
<p>When in doubt, print the numerical byte values, hexdump, etc.</p></pre>vpol: <pre><p>devil is in details. could you provide buildable code example?</p></pre>kpurdon: <pre><p>To answer your question, no string comparison is as you expect. There must be something causing your issue that your example is not showing us.</p></pre>whizack: <pre><p>looking at your example code there's a lot missing. I was not able to reproduce the issue you're describing with this reduction: <a href="https://play.golang.org/p/IH04cbFPjB" rel="nofollow">https://play.golang.org/p/IH04cbFPjB</a></p>
<p>you'll need to provide a more concrete example</p></pre>Morgahl: <pre><p>Given what you have shared there is nothing wrong with your expectations. We may need a more exact set of code to look at.
When posting code here you can either look into reddit's code formatting style OR paste a functioning snippet into <a href="https://play.golang.org" rel="nofollow">https://play.golang.org</a>.</p>
<p>Golang related tips, feel free to ignore these at your leasure just intending to help out a bit. :)</p>
<p>Error strings tend to not contain capitalization or punctuation. This comes from the common practice of:</p>
<pre><code>func Foo() error {
err := funcThatReturnsError()
if err != nil {
return fmt.Errorf("foo error: %s", err)
}
return nil
}
</code></pre>
<p>Although not really required by anybody, the Go community tends to use camelCase variable names.</p></pre>0xjnml: <pre><p>@vpol is right, please put a self contained reproducer on <a href="https://play.golang.org/" rel="nofollow">https://play.golang.org/</a>.</p></pre>adamkw94: <pre><p><a href="https://golang.org/pkg/strings/#Compare" rel="nofollow">https://golang.org/pkg/strings/#Compare</a>
Why not just use this </p></pre>dchapes: <pre><p>Did you read the problem description? Did you read <a href="https://golang.org/pkg/strings#Compare" rel="nofollow">the documentation for <code>strings.Compare</code></a>?</p>
<blockquote>
<p>Compare is included only for symmetry with package bytes. It is usually clearer and always faster to use the built-in string comparison operators ==, <, >, and so on.</p>
</blockquote></pre>
这是一个分享于 的资源,其中的信息可能已经有所发展或是发生改变。
入群交流(和以上内容无关):加入Go大咖交流群,或添加微信:liuxiaoyan-s 备注:入群;或加QQ群:692541889
0 回复
- 请尽量让自己的回复能够对别人有帮助
- 支持 Markdown 格式, **粗体**、~~删除线~~、
`单行代码`
- 支持 @ 本站用户;支持表情(输入 : 提示),见 Emoji cheat sheet
- 图片支持拖拽、截图粘贴等方式上传