1. 程式人生 > >go語言中空結構體的使用

go語言中空結構體的使用

簡介
go語言中的struct是一種很重要的語法,裡面可以存放各種欄位,當然它裡面也可以為空。這樣可以用來單純的做控制資訊。
示例

type Cli struct {
    Request *http.Request
    ReqBody []byte
    N int
    C int
    QPS float64
    M string
    Req *http.Request

    Writer io.Writer
    start time.Time
    stopChan chan struct{}//設計一個空的結構體用來做通道訊息
    result chan *result
    report *report
}

這因為struct空的大小為0,所以這樣可以在一定程度上減少記憶體使用,特別是在訊息管道開闢數量到達一定量級之後。