Help understanding this. I'm new to programming

xuanbao · · 460 次点击    
这是一个分享于 的资源,其中的信息可能已经有所发展或是发生改变。
<p>Why when you print this fmt.Println(&#34;Hello world&#34;[1])</p> <p>I get 101 instead of e</p> <p>Can someone explain this to me please</p> <hr/>**评论:**<br/><br/>rauyran: <pre><p>&#34;Hello world&#34;[1] returns a byte. The byte representing &#39;e&#39; is 101</p></pre>Diddyshyne120: <pre><p>This might be a stupid question, I don&#39;t get where the 101 came. Can you explain a lil bit more I can understand it. I&#39;m very new to this.</p></pre>adamtanner: <pre><p>Go source code (and thus string literals) is defined to be UTF-8 encoded. Each code point (character) is represented by a sequence of 1-4 bytes. The code point for &#34;e&#34; is represented by a single byte with the value &#34;01100101&#34;. When you interpret that byte as decimal you get the number &#34;101&#34;.</p> <p>You will often find the specific byte patterns represented as hexidecimal numbers rather than a decimals, so you may also see places that refer to &#34;e&#34; as &#34;65&#34;.</p> <p><a href="https://en.m.wikipedia.org/wiki/UTF-8" rel="nofollow">https://en.m.wikipedia.org/wiki/UTF-8</a><br/> <a href="https://blog.golang.org/strings" rel="nofollow">https://blog.golang.org/strings</a> </p></pre>HelperBot_: <pre><p>Non-Mobile link: <a href="https://en.wikipedia.org/wiki/UTF-8" rel="nofollow">https://en.wikipedia.org/wiki/UTF-8</a></p> <hr/> <p><sup>HelperBot</sup> <sup>v1.1</sup> <sup><a href="/r/HelperBot_" rel="nofollow">/r/HelperBot_</a></sup> <sup>I</sup> <sup>am</sup> <sup>a</sup> <sup>bot.</sup> <sup>Please</sup> <sup>message</sup> <sup><a href="/u/swim1929" rel="nofollow">/u/swim1929</a></sup> <sup>with</sup> <sup>any</sup> <sup>feedback</sup> <sup>and/or</sup> <sup>hate.</sup> <sup>Counter:</sup> <sup>89060</sup></p></pre>a_k_w: <pre><p>there is a difference between typed and raw strings, and in this context that means something.</p></pre>mrxinu: <pre><p>Have a look at my reply on your Twitter. Hope this helps!</p> <p><a href="https://twitter.com/mrxinu/status/883730005691322368" rel="nofollow">https://twitter.com/mrxinu/status/883730005691322368</a></p></pre>nuulss: <pre><p>when indexing strings, you get a single byte which can be converted back to a string <a href="https://play.golang.org/p/JQhAl7mI2V" rel="nofollow">https://play.golang.org/p/JQhAl7mI2V</a> <a href="https://golang.org/ref/spec#String_types" rel="nofollow">https://golang.org/ref/spec#String_types</a></p></pre>Diddyshyne120: <pre><p>Returned the value for the whole of the string which equal to 101?</p></pre>jimijiim: <pre><p><a href="https://golang.org/ref/spec#String_types" rel="nofollow">https://golang.org/ref/spec#String_types</a></p> <blockquote> <p>A string&#39;s bytes can be accessed by integer indices 0 through len(s)-1.</p> </blockquote></pre>abhayakara: <pre><p>The string is stored as a sequence of numbers in an array. The numbers are <a href="http://www.asciitable.com/" rel="nofollow">ASCII</a> (in this case) or utf-8 (more generally).</p> <p>So the array that is that string contains: 72 101 108 108 111 32 119 111 114 108 100</p> <p>As you can see, the 1th element (the second, because the 0th element is the first) is 101.</p></pre>

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

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