Should beginners be taught with ASCII or go straight to UTF-8?

agolangf · · 424 次点击    
这是一个分享于 的资源,其中的信息可能已经有所发展或是发生改变。
<p>I&#39;m working through the <a href="http://exercism.io/languages/go/exercises" rel="nofollow">Go exercises on exercism.io</a> and am surprised they&#39;re so ASCII-centric. Many of the exercise tests include no UTF-8 inputs, meaning that most people&#39;s submitted solutions will fail on a string that contains multi-byte characters. I&#39;m going through and <a href="http://exercism.io/submissions/f7c67b0f88954619995cc2d284d3af2a" rel="nofollow">adding multi-byte characters to the tests myself</a>.</p> <p>Anyone have any opinions about this? Is it OK for beginners to learn to handle strings in a Unicode-unsafe way at first? Or are they just picking up bad habits? Is Unicode-safe Go code just as easy to write as Unicode-unsafe code?</p> <hr/>**评论:**<br/><br/>neoasterisk: <pre><p>If you write your Go code correctly then it should also be able to handle UTF-8. If it can only handle ASCII then it is simply wrong code. I don&#39;t think the code itself that works for UTF-8 has anything difficult for beginners which is good. </p> <p>Nevertheless, understanding how strings work in Go can be tricky. I&#39;ve read <a href="https://blog.golang.org/strings" rel="nofollow">https://blog.golang.org/strings</a> many times and there&#39;s still stuff I miss.</p></pre>sin2pifx: <pre><blockquote> <p>If you write your Go code correctly</p> </blockquote> <p>I think that&#39;s OP&#39;s point. Personally, I&#39;d vote for teaching them both, but only if they&#39;re up to understanding the difference between ASCII, byte, UTF-8 and Unicode. If they&#39;re not there yet, stick with UTF-8.</p></pre>stuartcarnie: <pre><p>As per usual, <em>it depends</em>. I disagree with the statement that </p> <blockquote> <p>If it can only handle ASCII then it is simply wrong code</p> </blockquote> <p>What if the requirements of the program are to decode hex encoded strings, parse a list of IPv4 addresses or sum Hindu-Arabic numerals? The input for these examples requires ASCII and therefore the solutions can legitimately index the strings using byte offsets. The program can still deal with unexpected data, as no valid multi-byte UTF-8 sequence includes 0x00-0x7f (ASCII)</p> <p>UTF-8 should be an entirely separate, lengthy discussion including the APIs necessary in dealing with UTF-8 data.</p></pre>ChristophBerger: <pre><p>I definitely vote for including UTF-8 into learning material right from the start. There are a couple of things that beginners might stumble over if they don&#39;t know about Unicode and the way Go handles it. </p> <p>For example: <code>for i=0; i &lt; len(str); i++</code> (byte-oriented) versus <code>for i, v := range str</code> (rune-oriented). </p> <p>Or <code>char := str[i]</code> where <code>i</code> might happen to be the index of one byte within a 3-bytes UTF-8 character. The student would see garbage in <code>char</code> and would have no idea why.</p></pre>Guitarbum722: <pre><p>It would be silly not to cover UTF-8. It is the most widely used now. Plus, all of the ASCII characters are completely handled in UTF-8 with no problem whatsoever. </p> <p>It isn&#39;t a difficult concept; it just needs to be explained well to the person learning. A programmer will encounter it in their first week on the job, so why not prepare them!</p></pre>Guitarbum722: <pre><p>Also string literals in Golang are UTF-8. </p></pre>rozzlapede: <pre><p>I&#39;m working on a technology-independent curriculum for web developer training and I&#39;m putting the unicode / UTF-8 material up front after HTML, alonside javascript and css. Not because they need to be experts in it, but because the class of bugs related to character encoding can appear in just about any context and can be especially difficult for beginners to troubleshoot on their own. Here are some tools I&#39;ve found to help with exercises:</p> <p>That great technophile video: <a href="https://youtu.be/MijmeoH9LT4" rel="nofollow">https://youtu.be/MijmeoH9LT4</a></p> <p>UniView for copy-paste inspection of Unicode text: <a href="https://r12a.github.io/uniview/" rel="nofollow">https://r12a.github.io/uniview/</a></p> <p>Mimic for generating unicode-bug-ridden syntax: <a href="https://github.com/reinderien/mimic" rel="nofollow">https://github.com/reinderien/mimic</a></p></pre>sairamk: <pre><p>I think it should be mentioned about UTF-8 existence and why and how its useful for non-ASCII scenarios and provide examples or questions where it can be applied like Regular expressions. </p> <p><del>The definition of <code>rune</code> actually covers UTF-8.</del></p> <p>Edit: strike through. see comment thread below</p></pre>4ad: <pre><blockquote> <p>The definition of <code>rune</code> actually covers UTF-8.</p> </blockquote> <p>No, rune represents Unicode code points, which have nothing to do with UTF-8.</p></pre>driusan: <pre><p>Unicode code points have <em>something</em> to do with UTF-8...</p></pre>4ad: <pre><p>No, they don&#39;t.</p> <p>UTF-8 is one possible encoding for Unicode.</p></pre>driusan: <pre><p>Your second sentence contradicts your first one.</p></pre>4ad: <pre><p>No, it does not. I recommend you try to understand the relation between things before speaking up. In particular relations between cause and effect, the relation between multiple levels of abstractions, and the relation between concepts and the representation of concepts.</p></pre>peterbourgon: <pre><p>Yo dude, chill ✌️

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

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