douzhao7445 2012-07-03 09:12
浏览 115
已采纳

Go是否接受变量中的空格,就像下划线一样? 它有什么作用? [重复]

This question already has an answer here:

Except prolog, I have never seen _ in modern languages mean something, mostly used in variable declarations such me_and_you_variable = bla bla.

What does this single _ means in Go? Is it a variable or what?

Will Go language have spaces in variable then, because now _ is a part of operator in the language (so confusing!!) ? e.g.

response, _, err := http.Get(kinopiko_flair)
</div>
  • 写回答

1条回答 默认 最新

  • dqm4977 2012-07-03 09:18
    关注

    _ in Go is a placeholder: in your case you simply don't need the second value returned from function http.Get(kinopiko_flair) so you simply throw it away.

    It's a variable used when you just need something to assign a value to but don't need that value afterwards.

    Also see this example from Effective Go:

    type ByteSize float64
    
    const (
        _           = iota // ignore first value by assigning to blank identifier
        KB ByteSize = 1 << (10 * iota)
        MB
        GB
        TB
        PB
        EB
        ZB
        YB
    )
    

    In this case the first value of special variable iota (which is 0) is not useful, so it's simply discarded: no memory will be used to store that value.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度
  • ¥30 关于#r语言#的问题:如何对R语言中mfgarch包中构建的garch-midas模型进行样本内长期波动率预测和样本外长期波动率预测
  • ¥15 ETLCloud 处理json多层级问题
  • ¥15 matlab中使用gurobi时报错
  • ¥15 这个主板怎么能扩出一两个sata口
  • ¥15 不是,这到底错哪儿了😭
  • ¥15 2020长安杯与连接网探
  • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么