Go中接口的類型查詢:comma-ok斷言和switch測(cè)試

// code_026_go_antic_package project main.go
<pre name="code" class="plain">package main

import (
    "container/list"
    "fmt"
    "math/rand" //備注2:隨機(jī)數(shù)的包
    "sync" //備注1:異步任務(wù)的包
    "time"
)

type INFO struct {
    lock sync.Mutex   //備注1:異步鎖
    Name string
    Time int64
}

var List *list.List = list.New() //備注3:初始化List變量

func main() {
    var Info INFO
    go func() {
        for i := 0; i < 5; i++ {
            time.Sleep(time.Duration(1e9 * int64(rand.Intn(5))))//備注2:隨機(jī)數(shù)rand.Intn(5)<---> 1e9為科學(xué)計(jì)數(shù)法,1 * 10的9次方
            Info.lock.Lock()//備注1:上鎖
            Info.Name = fmt.Sprint("Name", i) //備注: Sprint采用默認(rèn)格式將其參數(shù)格式化,串聯(lián)所有輸出生成并返回一個(gè)字符串。如果兩個(gè)相鄰的參數(shù)都不是字符串,會(huì)在它們的輸出之間添加空格
            Info.Time = time.Now().Unix() + 3
            Info.lock.Unlock()//備注1:解鎖
            List.PushBack(Info)//備注3:List表達(dá)式調(diào)用
        }
    }()
    go Getgoods()
    select {}
}
func Getgoods() {
    for {
        time.Sleep(1e8)
        for List.Len() > 0 {//備注3:List對(duì)象的使用
            N, T := List.Remove(List.Front()).(INFO).name() //備注3:List對(duì)象的使用和value.(type)的妙用
            now := time.Now().Unix() //備注4:獲取當(dāng)前日期轉(zhuǎn)換后的時(shí)間戳
            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
}

網(wǎng)站標(biāo)題:Go中接口的類型查詢:comma-ok斷言和switch測(cè)試
文章路徑:http://muchs.cn/article14/pdjcde.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供移動(dòng)網(wǎng)站建設(shè)、營(yíng)銷型網(wǎng)站建設(shè)網(wǎng)站制作、小程序開(kāi)發(fā)、網(wǎng)站改版靜態(tài)網(wǎng)站

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來(lái)源: 創(chuàng)新互聯(lián)

成都網(wǎng)頁(yè)設(shè)計(jì)公司