咖啡伴侶

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

          2013年10月10日

          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): //超時(shí)5s
                  fmt.Println("5 sec call")
              }
          }

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

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

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

          下面這個(gè)例子解釋這個(gè)問(wèn)題

          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)
          }

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

          所以看到庫(kù)里接口做參數(shù)類型定義的時(shí)候,可以簡(jiǎn)單認(rèn)為,這個(gè)接口肯定是個(gè)對(duì)象指針(雖然也可以用對(duì)象,單估計(jì)沒(méi)有哪個(gè)類庫(kù)會(huì)用)

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

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

          posted @ 2013-12-22 12:45 oathleo 閱讀(4401) | 評(píng)論 (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() { //非阻塞
                  
          //后續(xù)每24小時(shí)建立目錄
                  ticker24h := time.NewTicker(5 * time.Second)
                  for {
                      select {
                      case <-ticker24h.C:
                          fmt.Println("print")
                      }
                  }
              })

              <-c
          }

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

          聲明:
          源slice= src
          添加slice = app
          結(jié)果slice=tar
          append時(shí)
          len tar === len src +   len app
          1)如果len(src) + len(app) <= cap(src)    cap tar  =   cap(src)
          2)否則 
                a) len(src) + len(app) > 2* cap(src)     cap tar  =   len(src) + len(app)
                b) cap(src) < len(src) + len(app) <= 2* cap(src)    cap tar = 2* cap(src)
              data := make([]int, 10, 20)
              data[0] = 1
              data[1] = 2

              dataappend := make([]int, 12, 30)//修改這個(gè)len 
              dataappend[0] = 1
              dataappend[1] = 2

              result := append(data, dataappend)

              result[0] = 99
              result[11] = 98

              fmt.Println("length:", len(data), "cap:", cap(data), ":", data)
              fmt.Println("result length:", len(result), "cap:", cap(result), ":", result)
              fmt.Println("length:", len(dataappend), "cap:", cap(dataappend), ":", dataappend)

          posted @ 2013-11-20 18:48 oathleo 閱讀(5266) | 評(píng)論 (1)編輯 收藏

          1.slice1:= slice[0:2]
          引用,非復(fù)制,所以任何對(duì)slice1或slice的修改都會(huì)影響對(duì)方
          data := []int{1, 2, 3, 4, 5, 6, 7, 8, 9, 0}
          data1 := data[0:2]
          data1[0] = 99
          fmt.Println(data1)
          fmt.Println(data)
          [99 2]
          [99 2 3 4 5 6 7 8 9 0]
          2.append
          append 比較特殊
          聲明:
          源slice= src
          添加slice = app
          結(jié)果slice=tar
          1)如果len(src) + len(app) <= cap(src)  src和tar 是指向同一數(shù)據(jù)引用 ,即修改src或tar,會(huì)影響對(duì)方
          2)否則 tar 是copy的方式 src + app ,即修改src或tar,不會(huì)影響對(duì)方
          無(wú)論哪種情況不會(huì)影響app,因?yàn)閍pp都會(huì)用copy的方式進(jìn)入tar
           
          func test2() {
          data := make([]int, 10, 20)
          data[0] = 1
          data[1] = 2
          dataappend := make([]int, 10, 20)//len <=10 則  result[0] = 99 會(huì) 影響源Slice
          dataappend[0] = 1
          dataappend[1] = 2
          result := append(data, dataappend...)
          result[0] = 99
          result[11] = 98
          fmt.Println("length:", len(data), ":", data)
          fmt.Println("length:", len(result), ":", result)
          fmt.Println("length:", len(dataappend), ":", dataappend)
          }

          posted @ 2013-11-20 18:46 oathleo 閱讀(6676) | 評(píng)論 (1)編輯 收藏

          index := bytes.IndexByte(buf_PN, 0)
          rbyf_pn := buf_PN[0:index]

          posted @ 2013-11-19 10:16 oathleo 閱讀(9017) | 評(píng)論 (0)編輯 收藏

          c := exec.Command("taskkill.exe", "/f", "/im", "test.exe")
          err := c.Start()

          posted @ 2013-11-15 14:07 oathleo 閱讀(6772) | 評(píng)論 (4)編輯 收藏

          s2 := append(s1, *)

          切片s1上記錄的切片信息復(fù)制給s2,

          1.如果s1指向的底層array長(zhǎng)度不夠,append的過(guò)程會(huì)發(fā)生如下操作:內(nèi)存中不僅新開辟一塊區(qū)域存儲(chǔ)append后的切片信息,而且需要新開辟一塊區(qū)域存儲(chǔ)底層array(復(fù)制原來(lái)的array至這塊新array中),最后再append新數(shù)據(jù)進(jìn)新array中,這樣,s2指向新array。

          2.如果s1指向的底層array長(zhǎng)度夠,
          s2和s1指向同一個(gè)array,append的結(jié)果是內(nèi)存中新開辟一個(gè)區(qū)域存儲(chǔ)新切片信息。

          開辟一塊區(qū)域存儲(chǔ)底層array 使用下面的策略:
          1.如果 增加的 len < s的cap 則 新s的cap*2
          2.如果 增加的 len > s的cap 則 新s的cap = 老cap + 增加數(shù)據(jù)的 len

          posted @ 2013-11-05 16:39 oathleo 閱讀(4525) | 評(píng)論 (0)編輯 收藏


           // (A)
          time.AfterFunc(5 * time.Minute, func() {
              fmt.Printf("expired")
          }

          // (B) create a Timer object
          timer := time.NewTimer(5 * time.Minute)
          <-timer.C
          fmt.Printf("expired")

          // (C) time.After() returns timer.C internally
          <-time.After(5 * time.Minute)
          fmt.Printf("expired")

          posted @ 2013-10-10 15:07 oathleo 閱讀(1672) | 評(píng)論 (0)編輯 收藏

          對(duì)亍非緩沖通道,“從通道接收數(shù)據(jù)”的操作
          一定會(huì)在 “向通道發(fā)送數(shù)據(jù)”的操作完成前發(fā)生。

          package main

          import (
              "fmt"
          )

          var c = make(chan int)
          var str string

          func ready() {
              str = "abc"
              fmt.Println("ready1")
              <-c //get
              fmt.Println("ready2")
          }

          func main() {
              go ready()
              c <- 1 //put
              fmt.Println(str)
          }

          ready1
          ready2
          abc

          posted @ 2013-10-10 10:56 oathleo 閱讀(1564) | 評(píng)論 (0)編輯 收藏

          主站蜘蛛池模板: 湟中县| 信丰县| 武邑县| 嘉禾县| 沧州市| 景谷| 明星| 铜梁县| 杨浦区| 新建县| 新田县| 云龙县| 山丹县| 同仁县| 和龙市| 保康县| 泰宁县| 青浦区| 朝阳县| 巩留县| 丰台区| 荃湾区| 清镇市| 景德镇市| 抚州市| 东阳市| 九龙城区| 赤壁市| 且末县| 南部县| 长春市| 防城港市| 常州市| 吴川市| 宣武区| 衢州市| 获嘉县| 拉孜县| 永靖县| 清苑县| 任丘市|