Go语言interface的value.(type)使用小技巧
发布时间:2020-12-16 18:41:23 所属栏目:大数据 来源:网络整理
导读: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++
<pre name="code" class="plain">package main import ( "container/list" "fmt" "math/rand" "sync" "time" ) type INFO struct { lock sync.Mutex Name string Time int64 } var List *list.List = list.New() func main() { var Info INFO go 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() + 3 Info.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,now) } } } func (i INFO) name() (string,int64) { return i.Name,i.Time } (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |