```golang
func main() {
x := int64(99)
bytesBuffer := bytes.NewBuffer([]byte{})
binary.Write(bytesBuffer, binary.BigEndian, x)
conn, err := net.DialTimeout("tcp", "localhost:9090",time.Second*30)
defer conn.Close()
if err != nil {
fmt.Printf("connect failed, err : %v\n", err.Error())
return
}
wg := sync.WaitGroup{}
wg.Add(1)
//启动传输协程
go func(){
inputReader := bufio.NewReader(os.Stdin)
for {
input, err := inputReader.ReadString('\n')
if err != nil {
fmt.Printf("read from console failed, err: %v\n", err)
break
}
trimmedInput := strings.TrimSpace(input)
if trimmedInput == "Q" {
break
}
_, err = conn.Write([]byte(trimmedInput))
if err != nil {
fmt.Printf("write failed , err : %v\n", err)
break
}
}
wg.Done()
}()
//启动接收协程
go func(){
result := bytes.NewBuffer(nil)
var buf [10]byte
for {
n, err := conn.Read(buf[0:])
result.Write(buf[0:n])
if err != nil {
fmt.Println(err)
if err == io.EOF {
}
break
}else{
fmt.Println("recview:",result.String())
}
result.Reset()
}
wg.Done()
}()
wg.Wait()
}
```
有疑问加站长微信联系(非本文作者)