Am I Missing Something About String Comparison?

agolangf · · 380 次点击    
这是一个分享于 的资源,其中的信息可能已经有所发展或是发生改变。
<p>Excuse the noob question, but I am only now getting started with the language. Any help is appreciated, since I haven&#39;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&#39;s test suite) whether that string is anything other than a given string (with the &#39;!=&#39; operator). This is always true (meaning the strings are always different) and an error is raised.</p> <p>In pseudocode, to understand what I&#39;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&#39;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 &#34;go test&#34; 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 &#34;thing&#34; doesn&#39;t act like a string. Take a look at this (this is how it&#39;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&#39;t return &#34;aaB&#34; but &#34;Ba&#34; (for a := MyFunction()). Maybe this helps?</p> <p>PS: I know the file directory doesn&#39;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 &#39;\r&#39; 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 &#39;\r&#39; 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&#39;s a lot missing. I was not able to reproduce the issue you&#39;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&#39;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&#39;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(&#34;foo error: %s&#34;, 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 ==, &lt;, &gt;, and so on.</p> </blockquote></pre>

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

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