<rt id="bn8ez"></rt>
<label id="bn8ez"></label>

  • <span id="bn8ez"></span>

    <label id="bn8ez"><meter id="bn8ez"></meter></label>

    咖啡伴侶

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

    置頂隨筆

    xml對應的struct 屬性必須大寫,否則無法實現!
    Code是必須的
    package main

    import (
        "encoding/xml"
        "fmt"
        "os"
    )

    type xmldas struct {
        XMLName  xml.Name       `xml:"das"`
        DataPort string         `xml:"DataPort,attr"`
        Desc     string         `xml:"desc,attr"`
        Src      xmlsource      `xml:"source"`
        Dest     xmldestination `xml:"destination"`
    }

    type xmlsource struct {
        Path  string `xml:"path,attr"`
        Param string `xml:"param,attr"`
    }

    type xmldestination struct {
        Path  string `xml:"path,attr"`
        Param string `xml:"param,attr"`
    }

    func main() {
        v := xmldas{DataPort: "8250", Desc: "123"}
        v.Src = xmlsource{Path: "123", Param: "456"}
        v.Dest = xmldestination{Path: "789", Param: "000"}
        output, err := xml.MarshalIndent(v, "  ", "    ")
        if err != nil {
            fmt.Printf("error: %v\n", err)
        }
        os.Stdout.Write([]byte(xml.Header))
        os.Stdout.Write(output)
    }

    posted @ 2013-08-16 15:53 oathleo 閱讀(2697) | 評論 (1)編輯 收藏

    2013年12月24日

    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)編輯 收藏

    2013年12月22日

    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 閱讀(4404) | 評論 (1)編輯 收藏

    2013年12月19日

    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 閱讀(5379) | 評論 (0)編輯 收藏

    2013年11月20日

    聲明:
    源slice= src
    添加slice = app
    結果slice=tar
    append時
    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)//修改這個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 閱讀(5268) | 評論 (1)編輯 收藏

    1.slice1:= slice[0:2]
    引用,非復制,所以任何對slice1或slice的修改都會影響對方
    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
    結果slice=tar
    1)如果len(src) + len(app) <= cap(src)  src和tar 是指向同一數據引用 ,即修改src或tar,會影響對方
    2)否則 tar 是copy的方式 src + app ,即修改src或tar,不會影響對方
    無論哪種情況不會影響app,因為app都會用copy的方式進入tar
     
    func test2() {
    data := make([]int, 10, 20)
    data[0] = 1
    data[1] = 2
    dataappend := make([]int, 10, 20)//len <=10 則  result[0] = 99 會 影響源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 閱讀(6678) | 評論 (1)編輯 收藏

    2013年11月19日

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

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

    2013年11月15日

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

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

    2013年11月5日

    s2 := append(s1, *)

    切片s1上記錄的切片信息復制給s2,

    1.如果s1指向的底層array長度不夠,append的過程會發生如下操作:內存中不僅新開辟一塊區域存儲append后的切片信息,而且需要新開辟一塊區域存儲底層array(復制原來的array至這塊新array中),最后再append新數據進新array中,這樣,s2指向新array。

    2.如果s1指向的底層array長度夠,
    s2和s1指向同一個array,append的結果是內存中新開辟一個區域存儲新切片信息。

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

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

    2013年10月10日


     // (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 閱讀(1674) | 評論 (0)編輯 收藏

    對亍非緩沖通道,“從通道接收數據”的操作
    一定會在 “向通道發送數據”的操作完成前發生。

    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 閱讀(1565) | 評論 (0)編輯 收藏

    主站蜘蛛池模板: 亚洲av无码一区二区三区网站| 久久精品国产精品亚洲艾| 亚洲?v无码国产在丝袜线观看 | 亚洲精品午夜无码电影网| 亚洲动漫精品无码av天堂| 亚洲中文无码a∨在线观看| 亚洲AV永久纯肉无码精品动漫| 国产精品永久免费| 亚洲成人在线免费观看| 日韩免费视频观看| 精品亚洲永久免费精品| 免费在线中文日本| 永久免费视频v片www| 日本红怡院亚洲红怡院最新| 在线观看片免费人成视频播放| 成人毛片18女人毛片免费| 亚洲中文字幕无码日韩| 亚洲日韩AV一区二区三区四区| 中文字幕无码免费久久9一区9 | 国产老女人精品免费视频| 亚洲福利视频导航| 黄页视频在线观看免费| 久久久久免费看成人影片| 亚洲AV永久无码精品一区二区国产 | 激情五月亚洲色图| 在线看片免费人成视频福利| 国产美女无遮挡免费视频| 美女无遮挡免费视频网站| 亚洲区小说区图片区QVOD| 成年网在线观看免费观看网址| 999在线视频精品免费播放观看| 国产亚洲精品国看不卡| 亚洲JIZZJIZZ妇女| 毛片免费观看的视频| 大片免费观看92在线视频线视频| 亚洲av永久无码精品古装片| 女人与禽交视频免费看| 国产精品久免费的黄网站| 久久久久亚洲精品天堂| 又大又硬又粗又黄的视频免费看| 99久久久国产精品免费蜜臀|