[日常] Go语言圣经--示例: 并发的Echo服务
最简单的回声服务器: import (
"io" "net" "log" ) func main() {
} func handleConn(c net.Conn) { 原理: 1.io.Copy()方法func Copy(dst Writer,src Reader) (written int64,err error) 2.net.Conn类型type Conn interface { Read(b []byte) (n int,err error) Write(b []byte) (n int,err error) ...}一个类型如果拥有一个接口需要的所有方法,那么这个类型就实现了这个接口 3.io.Writertype Writer interface { Write(p []byte) (n int,err error)}4.io.Readertype Reader interface { Read(p []byte) (n int,err error)} 升级版,每条连接一个goroutine,每个goroutine中分出多个输出goroutine import (
"bufio" "fmt" "log" "net" "strings" "time" ) func main() { func handleConn(c net.Conn) {
1.fmt.Fprintln()func Fprintln(w io.Writer,a ...interface{}) (n int,err error) 2.bufio.NewScanner() func NewScanner(r io.Reader) *Scanner func (s *Scanner) Scan() bool func (s *Scanner) Text() string 也用到了大量的7.3节 实现接口的条件 (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |