ARTS-S golang goroutines and channels

向tcp服务端发消息

package main

import (
	"io"
	"log"
	"net"
	"os"
)

func main() {
	conn, err := net.Dial("tcp", "localhost:8080")
	defer conn.Close()
	if err != nil {
		log.Fatal(err)
	}
	go mustCopy(os.Stdout, conn)
	mustCopy(conn, os.Stdin)
}

func mustCopy(dst io.Writer, src io.Reader)  {
	if _, err := io.Copy(dst, src); err != nil {
		log.Printf("mustCopy error:%v", err)
	}
}

posted on 2019-09-30 23:43  荷楠仁  阅读(158)  评论(0编辑  收藏  举报

导航