[]uint8 to string and compare

agolangf · · 1113 次点击    
这是一个分享于 的资源,其中的信息可能已经有所发展或是发生改变。
<p>Hello, for the context my company asked me to studie the golang in order to be ready for a next project</p> <p>As i&#39;m realizing a cool example, i&#39;m having an issue wich I don&#39;t understand neither in the doc or on the internet</p> <p>1] I hthp.get a .m3u8 file and get []uint8 data &lt;br&gt; 2] I work on this data by changing them into string &lt;br&gt; 3] I just would like to say &#34;if this []uint8 data is equal to &#34;string&#34; &lt;br&gt;</p> <p>But I don&#39;t understand why I can&#39;t trigger this condition</p> <p>This is my cmd ouput</p> <p>//the value of my data from my .m3u8 file</p> <h1>EXT-X-ENDLIST</h1> <p>//the type of my data from my .m3u8 file []uint8</p> <p>this is my code</p> <pre><code>func main() { resp, err := http.Get(&#34;the_url_of_the_file.m3u8&#34;) if err != nil { // err } reader := bufio.NewReader(resp.Body) for { line, err := reader.ReadBytes(&#39;\n&#39;) if err != nil { } if string(line) == (&#34;#EXT-X-ENDLIST&#34;) { //trigger } } } </code></pre> <p>If someone have juste a clue to help me understand I tried a lot of different fonction in the doc but it&#39;s seems it&#39;s a basic thing that I didn&#39;t get</p> <p>Thanks</p> <hr/>**评论:**<br/><br/>TheMerovius: <pre><p>The issue is (probably), that <code>ReadBytes</code> returns everything up to <em>and including</em> the delimiter, so <code>line</code> will include the newline (and comparison fails, as your string doesn&#39;t).</p> <p>Which causes me to re-emphasize that you should almost always, when reading line-wise input, just use <code>bufio.Scanner</code>. It takes care of all of that for you and should work as expected. I.e.</p> <pre><code>func main() { resp, err := http.Get(&#34;the_url_of_the_file.m3u8&#34;) if err != nil { log.Fatal(err) } defer body.Close() s := bufio.NewScanner(resp.Body) for s.Scan() { line := s.Text() if line == &#34;#EXT-X-ENDLIST&#34; { //trigger } } if err := s.Err(); err != nil { log.Fatal(err) } } </code></pre></pre>IpreferWater: <pre><p>Thank you very much for this quick and clear explication </p> <p>worked perfectly </p></pre>DeedleFake: <pre><p>Something else to keep in mind is that Go does a copy when you convert from byte slice to string and vice versa as a way of guaranteeing string immutability, which means that calling <code>Scanner.Text()</code> does an allocation and a copy on every loop. If you don&#39;t need to actually use the string that you&#39;re checking there, you could alternatively use <code>Scanner.Bytes()</code> and then use <a href="https://godoc.org/bytes#Equal" rel="nofollow"><code>bytes.Equal()</code></a> to check for the equality.</p> <p>Probably a bit of a premature optimization in this case, but just something that&#39;s good to keep in mind.</p></pre>elagergren: <pre><p>Definitely not premature. This is a <em>good</em> example of a simple, obvious (as in the opposite of, &#34;why is this code doing this? ...oh, it&#39;s an optimization&#34;) optimization.</p></pre>

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

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