咖啡伴侶

          呆在上海
          posts - 163, comments - 156, trackbacks - 0, articles - 2

          2013年12月19日

          package main

          import (
              "fmt"
              "time"
          )

          var ch chan int = make(chan int, 1)

          func main() {
              go aaa()

              select {
              case <-ch: //拿到鎖
                  fmt.Println("call")
              case <-time.After(5 * time.Second): //超時5s
                  fmt.Println("5 sec call")
              }
          }

          func aaa() {
              time.Sleep(time.Second * 3)
              ch <- 1
          }

          posted @ 2013-12-24 13:03 oathleo 閱讀(7304) | 評論 (0)編輯 收藏

          conn, err = ln.Accept()
          go handleConnection(conn)
          看到這里我曾經有個疑問,為什么不是  handleConnection(&conn) ?

          下面這個例子解釋這個問題

          package main

          import (
              "fmt"
          )

          type Interface interface {
              say() string
          }

          type Object struct {
          }

          func (this *Object) say() string {
              return "hello"
          }

          func do(i Interface) string {
              return i.say()
          }

          func main() {
              o := Object{}
              fmt.Println(do(&o))
              fmt.Printf("CCCCCCCCCCC:%T", o)
          }

          函數的參數以接口定義,編譯器會自己判斷參數是對象還是對象的指針
          比如,say是指針上的方法,所以do只接受Object的指針做參數,do(o)是編譯不過的

          所以看到庫里接口做參數類型定義的時候,可以簡單認為,這個接口肯定是個對象指針(雖然也可以用對象,單估計沒有哪個類庫會用)

          例如:
          conn, err = ln.Accept()
          go handleConnection(conn)

          這里conn是個接口,不需要 go handleConnection(&conn)

          posted @ 2013-12-22 12:45 oathleo 閱讀(4401) | 評論 (1)編輯 收藏

          package main

          import (
              "fmt"
              "mag/common"
              "time"
          )

          func main() {
              c := make(chan bool, 10)

              tt := common.GetTodayGivenTime("161300")
              dd := common.SinceNow(tt)
              time.AfterFunc(dd, func() { //非阻塞
                  
          //后續每24小時建立目錄
                  ticker24h := time.NewTicker(5 * time.Second)
                  for {
                      select {
                      case <-ticker24h.C:
                          fmt.Println("print")
                      }
                  }
              })

              <-c
          }

          posted @ 2013-12-19 16:15 oathleo 閱讀(5378) | 評論 (0)編輯 收藏

          主站蜘蛛池模板: 高尔夫| 蚌埠市| 文水县| 云安县| 基隆市| 聂拉木县| 金乡县| 鄢陵县| 甘德县| 黄冈市| 迭部县| 钦州市| 那曲县| 广饶县| 平顶山市| 霍州市| 敦化市| 肃南| 澳门| 金华市| 凭祥市| 永德县| 安陆市| 成武县| 弋阳县| 涿鹿县| 黔江区| 杭锦后旗| 松桃| 永川市| 磐石市| 苍梧县| 多伦县| 南昌市| 尉犁县| 肇源县| 太和县| 瑞丽市| 禄丰县| 迭部县| 治多县|