<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'm realizing a cool example, i'm having an issue wich I don't understand neither in the doc or on the internet</p>
<p>1] I hthp.get a .m3u8 file and get []uint8 data <br>
2] I work on this data by changing them into string <br>
3] I just would like to say "if this []uint8 data is equal to "string" <br></p>
<p>But I don't understand why I can'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("the_url_of_the_file.m3u8")
if err != nil {
// err
}
reader := bufio.NewReader(resp.Body)
for {
line, err := reader.ReadBytes('\n')
if err != nil {
}
if string(line) == ("#EXT-X-ENDLIST") {
//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's seems it's a basic thing that I didn'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'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("the_url_of_the_file.m3u8")
if err != nil {
log.Fatal(err)
}
defer body.Close()
s := bufio.NewScanner(resp.Body)
for s.Scan() {
line := s.Text()
if line == "#EXT-X-ENDLIST" {
//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't need to actually use the string that you'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'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, "why is this code doing this? ...oh, it's an optimization") optimization.</p></pre>
这是一个分享于 的资源,其中的信息可能已经有所发展或是发生改变。
入群交流(和以上内容无关):加入Go大咖交流群,或添加微信:liuxiaoyan-s 备注:入群;或加QQ群:692541889
- 请尽量让自己的回复能够对别人有帮助
- 支持 Markdown 格式, **粗体**、~~删除线~~、
`单行代码`
- 支持 @ 本站用户;支持表情(输入 : 提示),见 Emoji cheat sheet
- 图片支持拖拽、截图粘贴等方式上传