中文亚洲精品无码_熟女乱子伦免费_人人超碰人人爱国产_亚洲熟妇女综合网

當(dāng)前位置: 首頁 > news >正文

設(shè)計做網(wǎng)站哪家公司好短期培訓(xùn)班學(xué)什么好

設(shè)計做網(wǎng)站哪家公司好,短期培訓(xùn)班學(xué)什么好,寶雞免費(fèi)做網(wǎng)站,武漢論壇東湖社區(qū)1. context 作用 context包的context的接口,主要是控制協(xié)程執(zhí)行上下文的時間,以及取消程序的執(zhí)行,以及上下文中傳遞數(shù)據(jù)等作用,golang中耗時或者需要協(xié)同的操作都會見到context的身影。 context有幾個常用的方法 1.1 context.B…

1. context 作用

context包的context的接口,主要是控制協(xié)程執(zhí)行上下文的時間,以及取消程序的執(zhí)行,以及上下文中傳遞數(shù)據(jù)等作用,golang中耗時或者需要協(xié)同的操作都會見到context的身影。

context有幾個常用的方法

1.1 context.Backgroud()

創(chuàng)建一個空白的,頂級的,不會被取消的上下文。

1.2 context.WithTimeout

創(chuàng)建一個有執(zhí)行時間限制的上下文
func WithTimeout(parent Context, timeout time.Duration) (Context, CancelFunc) {
return WithDeadline(parent, time.Now().Add(timeout))
}
可以通過ctx.Done()方法獲取上下超時的通知。

package mainimport ("context""fmt""time"
)func main() {parentCxt := context.Background()ctx, cancel := context.WithTimeout(parentCxt, time.Second*5)go longTimeTask(ctx)time.Sleep(time.Second * 10)cancel()fmt.Println("task cancel success")
}func longTimeTask(ctx context.Context) {for {//fmt.Println("ok")select {case <-time.After(time.Second * 1):fmt.Println("task compete")case <-ctx.Done():fmt.Println("time out")return}}
}

1.3 context.WitchCancel(parentContext)

獲取一個可以中止的上下文,該方法會返回一個新的context,和cancel函數(shù),調(diào)用cancel函數(shù)后,通過ctx.Done()方法可以獲取到上下文取消的通知

package mainimport ("context""fmt""time"
)func main() {parentCxt := context.Background()ctx, cancel := context.WithCancel(parentCxt)go longTimeTask(ctx)time.Sleep(time.Second * 10)cancel()fmt.Println("task cancel success")
}func longTimeTask(ctx context.Context) {for {//fmt.Println("ok")select {case <-time.After(time.Second * 1):fmt.Println("task compete")case <-ctx.Done():fmt.Println("time out")return}}
}

1.4 context.WithValue()

func WithValue(parent Context, key, val any) Context {
if parent == nil {
panic(“cannot create context from nil parent”)
}
if key == nil {
panic(“nil key”)
}
if !reflectlite.TypeOf(key).Comparable() {
panic(“key is not comparable”)
}
return &valueCtx{parent, key, val}
}
可以在上下文中存貯一些參數(shù),通過上下文隨時獲取。

2.chan 信道

golang的chan和map,切片,接口,函數(shù)一樣是引用類型。
golang更加推薦使用chan去解決并發(fā)的協(xié)作的問題,對chan的讀寫是并發(fā)安全的,當(dāng)然你也可也以使用sync.Mutex等包來控制并發(fā)。

http://www.risenshineclean.com/news/38208.html

相關(guān)文章:

  • 遼寧城鄉(xiāng)建設(shè)部網(wǎng)站首頁網(wǎng)站策劃是干什么的
  • 內(nèi)網(wǎng)小網(wǎng)站的建設(shè)廣州網(wǎng)站運(yùn)營
  • 仿站能被百度收錄嗎招商外包
  • 主流網(wǎng)站開發(fā)軟件優(yōu)秀網(wǎng)站
  • 做二手車有哪些網(wǎng)站有哪些競價推廣代運(yùn)營
  • 本地網(wǎng)站建設(shè)多少錢信息大全百度推廣怎么開戶
  • 素材下載網(wǎng)站源碼seo網(wǎng)絡(luò)推廣企業(yè)
  • 上海微網(wǎng)站公司實(shí)時熱搜
  • 北京市環(huán)境建設(shè)辦公室網(wǎng)站免費(fèi)關(guān)鍵詞排名優(yōu)化軟件
  • 網(wǎng)站備案備的是域名還是空間企業(yè)培訓(xùn)有哪些方面
  • 深圳做網(wǎng)站哪家便宜微信小程序開發(fā)公司
  • 滄州wap網(wǎng)站制作企業(yè)推廣網(wǎng)
  • 小程序網(wǎng)站開發(fā)怎么樣谷歌廣告上海有限公司
  • 做外貿(mào)怎么打開國外網(wǎng)站亞馬遜關(guān)鍵詞搜索工具
  • 想自己做點(diǎn)飄紗素材到網(wǎng)站上買鄭州seo服務(wù)技術(shù)
  • 網(wǎng)站自助授權(quán)系統(tǒng)站長之家網(wǎng)站排名
  • 成立一個網(wǎng)站平臺要多少錢關(guān)鍵詞是怎么排名的
  • 品牌網(wǎng)站建設(shè)小科6a蚪湖北網(wǎng)絡(luò)推廣有限公司
  • 做網(wǎng)站要注意哪些長春網(wǎng)絡(luò)優(yōu)化最好的公司
  • 網(wǎng)站建設(shè)需要多大的空間百度數(shù)據(jù)庫
  • 網(wǎng)站搜索排名百度seo系統(tǒng)
  • 免費(fèi)看電視劇的網(wǎng)站在線觀看seo免費(fèi)診斷電話
  • 橙子建站落地頁制作郴州網(wǎng)站建設(shè)
  • 網(wǎng)站app簡單做軟文怎么寫
  • 北京價格微網(wǎng)站建設(shè)手機(jī)百度ai入口
  • 中英文網(wǎng)站asp怎么做搜狗搜圖
  • 網(wǎng)站建設(shè)頤高上海街保溫杯軟文營銷300字
  • 源代碼做的網(wǎng)站好用么百度開放云平臺
  • 湖州企業(yè)做網(wǎng)站手機(jī)關(guān)鍵詞seo排名優(yōu)化
  • 游戲推廣網(wǎng)站制作seo的基本步驟包括哪些