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

前端之家收集整理的这篇文章主要介绍了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,now)
		}
	}
}

func (i INFO) name() (string,int64) {
	return i.Name,i.Time
}
原文链接:https://www.f2er.com/go/190322.html

猜你在找的Go相关文章