From: acevest Date: Fri, 2 Jun 2017 09:17:24 +0000 (+0800) Subject: ... X-Git-Url: http://zhaoyanbai.com/repos/man.rndc.html?a=commitdiff_plain;h=3fb6cf9c019030ffb16654c9a280db26d98bfcb0;p=acecode.git ... --- diff --git a/learn/go/clock.go b/learn/go/clock.go new file mode 100644 index 0000000..c7ce67a --- /dev/null +++ b/learn/go/clock.go @@ -0,0 +1,52 @@ +/* + * ------------------------------------------------------------------------ + * File Name: clock.go + * Author: Zhao Yanbai + * 2017-05-29 12:35:49 星期一 CST + * Description: none + * ------------------------------------------------------------------------ + */ + +package main + +import ( + "io" + "fmt" + "net" + "log" + "time" +) + +func main() { + defer fmt.Println("Clock Server Exited..") + + listener, err := net.Listen("tcp", ":8000") + if err != nil { + log.Fatal(err) + } + + for { + conn, err := listener.Accept() + if err != nil { + log.Print(err) + continue + } + + go handleConn(conn) + } +} + + +func handleConn(c net.Conn) { + defer c.Close() + + for { + _, err := io.WriteString(c, time.Now().Format("2006-01-02 15:04:05\n")) + if err != nil { + return + } + + //time.Sleep(1 * time.Second) + time.Sleep(100 * time.Millisecond); + } +} diff --git a/learn/go/panic.go b/learn/go/panic.go new file mode 100644 index 0000000..a065032 --- /dev/null +++ b/learn/go/panic.go @@ -0,0 +1,34 @@ +/* + * ------------------------------------------------------------------------ + * File Name: panic.go + * Author: Zhao Yanbai + * 2017-05-20 12:17:13 星期六 CST + * Description: none + * ------------------------------------------------------------------------ + */ + +package main + +import ( + "fmt" + "os" + "runtime" +) + +func main() { + defer fmt.Println("Program Exited...") + defer printStack() + panicFunc(3) +} + +func printStack() { + var buf [4096]byte + n := runtime.Stack(buf[:], false) + os.Stdout.Write(buf[:n]) +} + +func panicFunc(n int) { + fmt.Printf("panicFunc(%d)\n", n+0/n) + defer fmt.Printf("defer panicFunc(%d)\n", n) + panicFunc(n - 1) +} diff --git a/learn/go/select.go b/learn/go/select.go new file mode 100644 index 0000000..c5cdee6 --- /dev/null +++ b/learn/go/select.go @@ -0,0 +1,67 @@ +/* + * ------------------------------------------------------------------------ + * File Name: select.go + * Author: Zhao Yanbai + * 2017-04-06 10:04:54 星期四 CST + * Description: none + * ------------------------------------------------------------------------ + */ +package main +import ( + "fmt" + "sync" + "time" + "math/rand" +) + +func main() { + + data := make(chan int, 1) + + var wg sync.WaitGroup + wg.Add(2) + + go func() { + defer wg.Done() + totalSize := 0 + for { + select { + case n := <-data: + fmt.Println("Received Data ", n) + totalSize += n + default: + fmt.Println("No Data Received") + } + fmt.Println("Received Total Size: ", totalSize) + time.Sleep(2*time.Second) + //time.Sleep(3*time.Millisecond) + } + }() + + + go func() { + defer wg.Done() + totalSend := 0 + n := 0 + for i :=0; i<100; i++ { + r := rand.Intn(100) + n += r + select { + case data<-n: + fmt.Println("Sent Data " , n) + totalSend += n + n = 0 + default: + fmt.Println("No Data Sent") + } + time.Sleep(1*time.Second) + //time.Sleep(1*time.Millisecond) + } + + fmt.Println("Sent Total Size: ", totalSend) + }() + + wg.Wait() +} + +