【发布时间】:2015-08-26 08:45:09
【问题描述】:
我发现自己经常使用渠道来阻止事情。在这些情况下,通道仅被用作一种信号方式,实际上并没有使用任何数据。
例如:
package main
import (
"fmt"
"time"
)
func routine(stopChan chan bool) {
fmt.Println("goroutine: I've started!")
<-stopChan
fmt.Println("goroutine: Cya'round pal")
}
func main() {
fmt.Println("main: Sample program run started")
stopChan := make(chan bool)
go routine(stopChan)
fmt.Println("main: Fired up the goroutine")
stopChan <- true
time.Sleep(1 * time.Second)
fmt.Println("main: Sample program run finished")
}
// Sample output:
//
// main: Sample program run started
// main: Fired up the goroutine
// goroutine: I've started!
// goroutine: Cya'round pal
// main: Sample program run finished
Run/view it 请在 golang 操场上。
我的问题是:
Go 中哪种通道类型的内存占用最少?
例如bool chan 需要的开销是否会比空 struct{} chan 少?
chan bool
chan byte
chan interface{}
chan struct{}
...
还有什么?
【问题讨论】:
-
我没有在源代码中进行验证,但我预计通道本身的开销会主导无缓冲通道的 byte、bool 或 struct{} 效果。
标签: memory go resources channel internals