Go语言interface的value.(type)使用小技巧

来源:互联网 发布:js input 样式 编辑:程序博客网 时间:2024/05/19 13:10
<pre name="code" class="plain">package mainimport ("container/list""fmt""math/rand""sync""time")type INFO struct {lock sync.MutexName stringTime int64}var List *list.List = list.New()func main() {var Info INFOgo func() {for i := 0; i < 5; i++ {time.Sleep(time.Duration(1e9 * int64(rand.Intn(5))))Info.lock.Lock()Info.Name = fmt.Sprint("Name", i)Info.Time = time.Now().Unix() + 3Info.lock.Unlock()List.PushBack(Info)}}()go Getgoods()select {}}func Getgoods() {for {time.Sleep(1e8)for List.Len() > 0 {N, T := List.Remove(List.Front()).(INFO).name()now := time.Now().Unix()if T-now <= 0 {fmt.Println(N, T, now)continue}time.Sleep(time.Duration((T - now) * 1e9))fmt.Println(N, T, now)}}}func (i INFO) name() (string, int64) {return i.Name, i.Time}

                                             
0 0
原创粉丝点击