Strings and []bytes - a couple of questions

agolangf · · 835 次点击    
这是一个分享于 的资源,其中的信息可能已经有所发展或是发生改变。
<p>I have read the official <a href="https://blog.golang.org/strings" rel="nofollow">blog post</a> but I&#39;m not much clearer on the difference? Also when is it better to use one or the other?</p> <hr/>**评论:**<br/><br/>djherbis: <pre><p>What is it that you&#39;re not understanding about the difference? A string is a read-only slice of bytes, but its also a distinct type. </p> <p>Can you share an example of when you think it wouldn&#39;t be clear of which to use? </p> <p>For the most part choosing which to use is as simple as deciding which section of the stdlib you are working with. If the functions you want to use take strings then use strings, otherwise use []byte. If you need to use both, choose the one which requires the least conversions, and just do string(byteSliceHere) or []byte(&#34;string here&#34;) to switch between the two.</p></pre>rebelnz: <pre><p>For example when I have to do anything with JSON - however I see what you mean about depending on section of stdlib: json.Unmarshal takes []byte <a href="http://golang.org/pkg/encoding/json/#Unmarshal" rel="nofollow">here</a> </p></pre>Darxide-: <pre><p>I&#39;m pretty new to Go and haven&#39;t really thought about how strings work, but I&#39;m guessing strings are a lot like an abstracted way of doing strings like in C. </p> <p>So example C string:</p> <pre><code>char s[20] = &#34;Hi this is a string&#34;; </code></pre> <p>I&#39;m assuming it&#39;s similar in Go where </p> <pre><code>var s [20]byte = &#34;Hi this is a string&#34; </code></pre> <p>is the exact same as </p> <pre><code>var s String = &#34;Hi this is a string&#34; </code></pre> <p>the second is just abstracted and contains functions to manipulate the string where as the first does not. - I could be wrong though!</p></pre>karlseguin: <pre><p>biggest difference is that strings are more like slices than arrays and they hold runes rather than bytes.</p> <p>This might help:</p> <p><a href="https://golang.org/pkg/reflect/#SliceHeader" rel="nofollow">https://golang.org/pkg/reflect/#SliceHeader</a></p> <p><a href="https://golang.org/pkg/reflect/#StringHeader" rel="nofollow">https://golang.org/pkg/reflect/#StringHeader</a></p></pre>djherbis: <pre><p>I was under the understanding that the string doesn&#39;t actually hold runes, but that runes were decoded from the string which is a read-only slice of bytes. <a href="https://blog.golang.org/strings" rel="nofollow">https://blog.golang.org/strings</a></p></pre>karlseguin: <pre><p>Sure. This might get pedantic, but if you do something like str[2] you&#39;ll get the 2nd rune. I&#39;d call the rune-aware nature of strings to be default/normal way strings operate. Is the only time you get the actual bytes (short of unsafe) when casting? </p> <p>We can at least agree that my wording was poor/bad.</p></pre>djherbis: <pre><p>Yeah, sorry I wasn&#39;t trying to get picky about the wording. Was just making sure I had the right idea since the op made mention to the actual memory layout of C strings.</p></pre>djherbis: <pre><p>Missed your question in the middle of this, the only time you get the runes is if you use a for range loop or if using: <a href="http://golang.org/pkg/unicode/utf8/" rel="nofollow">http://golang.org/pkg/unicode/utf8/</a></p> <p>Doing str[2] will get you the 3rd byte.</p> <p>For example: <a href="https://play.golang.org/p/1U8LSSSj_e" rel="nofollow">https://play.golang.org/p/1U8LSSSj_e</a></p></pre>djherbis: <pre><p>Go actually makes a distinction between slices and arrays. <a href="http://blog.golang.org/go-slices-usage-and-internals" rel="nofollow">http://blog.golang.org/go-slices-usage-and-internals</a></p> <p>In Go it&#39;s more like:</p> <pre><code>var s []byte = []byte(&#34;Hi this is a string&#34;) </code></pre> <p>is <em>almost</em> the same as</p> <pre><code>var s string = &#34;Hi this is a string&#34; </code></pre> <p>They are both essentially a slice of bytes, but Go&#39;s typing means that they are not <em>exactly</em> the same, switching between the two requires a type conversion.</p> <p><a href="https://golang.org/ref/spec#Conversions" rel="nofollow">https://golang.org/ref/spec#Conversions</a></p></pre>

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

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