JieLinDee的专栏

天道酬勤,厚积薄发.对Go语言有兴趣的朋友可以加:259693140(此群不允许闲聊)

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

<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, T, now)
		}
	}
}

func (i INFO) name() (string, int64) {
	return i.Name, i.Time
}


            
阅读更多
版权声明:本文为博主原创文章,交流邮箱czxichen@163.com,交流群:259693140(此群不允许闲聊) https://blog.csdn.net/fyxichen/article/details/46833895
文章标签: golang interface
个人分类: Golang
想对作者说点什么? 我来说一句

没有更多推荐了,返回首页

加入CSDN,享受更精准的内容推荐,与500万程序员共同成长!
关闭
关闭