golang socket TCPServer 读取每行

 bufio.NewReader

通过  ReadLine读取每一行

func (t *TcpSocketUtil) syncreciveline() (string, error) {
	buf := bufio.NewReader(net.conn)
	for {
		data, _, eof := buf.ReadLine()
		if eof == io.EOF {
			break
		} else if eof != nil {
			fmt.Println(eof)
			return "", eof
		}
		fmt.Println(string(data))
	}
	return "", nil
}

  通过 ReadBytes或者ReadSlice读取每一行

func (t *TcpSocketUtil) syncreciveline() (string, error) {
	buf := bufio.NewReader(net.conn)
	for {
		data, _, eof := buf.ReadBytes('\n')
		if eof == io.EOF {
			break
		} else if eof != nil {
			fmt.Println(eof)
			return "", eof
		}
		fmt.Println(string(data))
	}
	return "", nil
}

  

 

posted @ 2020-09-17 12:03  LLSix  阅读(763)  评论(0编辑  收藏  举报