douhui1630 2012-12-27 21:37
浏览 31
已采纳

为什么通道类型中带有“ <-”?

This works.

var tick <-chan time.Time = time.Tick(1e8)

However, this does not.

var tick chan time.Time = time.Tick(1e8)

Why do I need a <- in my type declaration for a channel? I thought that <- was for writing to or reading from a channel. Why would it appear in a type?

  • 写回答

3条回答 默认 最新

  • douzhang8840 2012-12-27 21:44
    关注

    Channels can have a type indicating whether it is readonly, writeonly or both.

    Indicating a channel direction is done with <- as part of the type or omitted for a read/write channel.

    So the <- in <-chan time.Time is part of the type,

    chan   time.Time  //Would be a read/writable channel
    chan<- time.Time  // Would be a write only channel
    <-chan time.Time  // Would be a read only channel
    

    and time.Tick(1e8) returns a read only channel.

    Read more in the language spec here

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 Python时间序列如何拟合疏系数模型
  • ¥15 求学软件的前人们指明方向🥺
  • ¥50 如何增强飞上天的树莓派的热点信号强度,以使得笔记本可以在地面实现远程桌面连接
  • ¥15 MCNP里如何定义多个源?
  • ¥20 双层网络上信息-疾病传播
  • ¥50 paddlepaddle pinn
  • ¥20 idea运行测试代码报错问题
  • ¥15 网络监控:网络故障告警通知
  • ¥15 django项目运行报编码错误
  • ¥15 STM32驱动继电器