我如何读取最多一对值?

How do I read up to a pair of values?

所以我有一个从 TCP 连接读取值的函数。我想阅读该行直到出现分隔符“\b\r”。到目前为止我有

func func1(someconnection net.Conn) string {
    c := bufio.NewReader(someconnection)
    buff1 := make([]byte, predefinedsizeofmessage)
    buff1, err := c.ReadBytes(byte('\r'))

    if err != nil {
        fmt.Printf("Connection closed")
    }

    message:= strings.Trim(string(buff1), delimiter)

    if len(message) == predefinedsizeofmessage {
        fmt.Printf("The message is in the wrong format")
    }

    fmt.Printf("The messageis: %s\n", message)

    return message
}

如果我在分隔符前读到 \r ,这显然是有问题的。我看过使用扫描仪的示例,但由于某种原因,客户端在使用时会调用超时。可能是我扫描器实现不当。

bufio.Reader 仅支持单字节定界符,您应该使用带有自定义拆分功能的 bufio.Scanner 来拆分 multi-byte 定界符。

可能是

的修改版本