国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > Golang > 正文

golang中單向channel的語法介紹

2020-04-01 19:04:33
字體:
來源:轉載
供稿:網友

本文主要給大家介紹的是關于golang/225691.html">golang單向channel語法的相關內容,分享出來供大家參考學習,下面話不多說,來一起看看詳細的介紹:

今天閑來無事補充一下golang的語法知識,想起來看看context的用法,結果碰到了一個沒見過的channel語法:

// A Context carries a deadline, cancelation signal, and request-scoped values// across API boundaries. Its methods are safe for simultaneous use by multiple// goroutines.type Context interface { // Done returns a channel that is closed when this `Context` is canceled // or times out. Done() <-chan struct{}  // Err indicates why this Context was canceled, after the Done channel // is closed. Err() error  // Deadline returns the time when this Context will be canceled, if any. Deadline() (deadline time.Time, ok bool)  // Value returns the value associated with key or nil if none. Value(key interface{}) interface{}}

注意看:Done() <- chan struct{} ,一個接口函數的聲明怎么這么奇怪呢?下面來分解一下。

Done() chan struct{} :如果函數定義改成這樣,其意義是,

  • 函數名Done,參數(),返回值chan struct{}
  • 單獨拿返回值來說,它是一個管道chan,內部的數據類型是struct{}
  • 單獨拿struct{}來說,我們熟悉type Name struct{a int, b bool}這樣去定義一個結構體的類型,其實struct{…}就是定義結構體,和map[string]int這種定義是一樣的,type只是給它啟了一個別名。

<- chan struct{} :單獨看這個表達式,我們知道如果ch := make(chan struct{}) ,那么<- ch是從管道里取出數據。但是chan struct{}是類型而不是變量,竟然能從一個類型里取數據??

其實<-chan int仍舊是一個管道類型,它叫做單向channel。如果是<-chan int,說明是只能讀不能寫的管道(也不能關閉),如果是chan <- int ,說明是只能寫不能讀的管道(可以關閉),僅此而已!

總結

以上就是這篇文章的全部內容了,希望本文的內容對大家的學習或者使用Go語言能帶來一定的幫助,如果有疑問大家可以留言交流,謝謝大家對VEVB武林網的支持。

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 绩溪县| 莲花县| 长顺县| 德格县| 祁连县| 大石桥市| 新闻| 陆丰市| 武陟县| 汽车| 大宁县| 普宁市| 延川县| 墨玉县| 潼关县| 隆子县| 阿图什市| 鄂伦春自治旗| 怀宁县| 临泽县| 江油市| 竹山县| 凤山市| 旌德县| 绿春县| 双辽市| 梁平县| 秦皇岛市| 红安县| 广西| 浑源县| 东丽区| 板桥市| 扶余县| 昌宁县| 诸暨市| 永寿县| 拜泉县| 湖南省| 鹤壁市| 若尔盖县|