咖啡伴侣

呆在上海
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 阅读(2674) | 评论 (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 阅读(7256) | 评论 (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 阅读(4372) | 评论 (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 阅读(5351) | 评论 (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 阅读(5234) | 评论 (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 阅读(6649) | 评论 (1)编辑 收藏

2013年11月19日

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

posted @ 2013-11-19 10:16 oathleo 阅读(8925) | 评论 (0)编辑 收藏

2013年11月15日

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

posted @ 2013-11-15 14:07 oathleo 阅读(6706) | 评论 (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 阅读(4502) | 评论 (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 阅读(1653) | 评论 (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 阅读(1544) | 评论 (0)编辑 收藏