ITPub博客

首页 > IT职业 > IT生活 > golang单向channel语法

golang单向channel语法

IT生活 作者:foxsi 时间:2021-09-09 09:54:29 0 删除 编辑

今天带大家来学习一个特殊的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() 

注意看Done()

  • Done() chan struct{}:如果函数定义改成这样,其意义是,

    • 函数名Done,参数(),返回值chan struct{}。

    • 单独拿返回值来说,它是一个管道chan,内部的数据类型是struct{}。

    • 单独拿struct{}来说,我们熟悉type Name struct{a int, b bool}这样去定义一个结构体的类型,其实struct{...}就是定义结构体,和map[string]int这种定义是一样的,type只是给它启了一个别名。

其实扩展阅读)

 


来自 “ ITPUB博客 ” ,链接:https://blog.itpub.net/4201/viewspace-2814220/ ,如需转载,请注明出处,否则将追究法律责任。

请登录后发表评论 登录
全部评论

注册时间:2019-08-12

  • 博文量
    74
  • 访问量
    523