<p>So i wrote this up and i am trying to have it read the reply from the server so it can do something using switch but it not working? It will print the message but for some reason its not using the switch?</p>
<pre><code> package main
import (
"fmt"
"net"
"bufio"
"os"
)
var hi bool = false
func main() {
conn, _ := net.Dial("tcp", "127.0.0.1:8080")
for {
if hi != true{//first connect
fmt.Fprintf(conn, "New" + "\n")
hi = true
}
message, _ := bufio.NewReader(conn).ReadString('\n')
switch message{
case "hello":
fmt.Print("hello")
case "DIE":
fmt.Print("Die")
os.Exit(0)
}
fmt.Println("Message from server: "+ message)
}
}
</code></pre>
<hr/>**评论:**<br/><br/>singron: <pre><p><code>ReadString</code> includes the delimiter, so you would have to match on <code>"hello\n"</code> and <code>"DIE\n"</code>.</p>
<p>Also, creating multiple bufio Readers from the same connection might lose data. For instance, the first one might read 2 statements into a buffer, you read the first one with ReadString, then the second Reader reads the third statement, and you never see the second statement.</p></pre>SaturnsVoid: <pre><p>I am not sure i understand, Would you be able to show an example, I am a visual learner sometimes :/</p></pre>mcvoid1: <pre><p>What message is printing where? The "New\n" you're sending to the server? The message printed at the bottom? If the message at the bottom is printing, and it's "hello" or "Die", you probably have some whitespace around the string that you should strip. If message is blank or something, you're probably getting an error from the server or reader that you're ignoring because you're dropping the errors instead of checking them.</p></pre>
这是一个分享于 的资源,其中的信息可能已经有所发展或是发生改变。
入群交流(和以上内容无关):加入Go大咖交流群,或添加微信:liuxiaoyan-s 备注:入群;或加QQ群:692541889
- 请尽量让自己的回复能够对别人有帮助
- 支持 Markdown 格式, **粗体**、~~删除线~~、
`单行代码`
- 支持 @ 本站用户;支持表情(输入 : 提示),见 Emoji cheat sheet
- 图片支持拖拽、截图粘贴等方式上传