Read whole data with Golang net.Conn.Read

前端 未结 3 805
时光取名叫无心
时光取名叫无心 2020-12-25 12:27

So I\'m building a network app in Go and I\'ve seen that Conn.Read reads into a limited byte array, which I had created with make([]byte, 2048) and

3条回答
  •  梦谈多话
    2020-12-25 13:06

    You can read data something like this:

    // import net/textproto
    import ("net/textproto", ...)
    
    ....
    
    reader := bufio.NewReader(Conn)
    tp := textproto.NewReader(reader)
    
    defer Conn.Close()
    
    for {
        // read one line (ended with \n or \r\n)
        line, _ := tp.ReadLine()
        // do something with data here, concat, handle and etc... 
    }
    ....
    

提交回复
热议问题