douping5015 2016-11-20 08:27
浏览 139
已采纳

golang为什么将类型说明符放在变量名之后?

Just out of curiosity, why does the golang place the type specifier after the variable name like below. Have to? Or happen to?

type person struct {
    name string
    age  int
}

Why not just like this? It's more natural IMHO, and it saves the type keyword.

struct person {
    string name
    int age
}
  • 写回答

1条回答 默认 最新

  • dpm91915 2016-11-20 08:53
    关注

    I think the Go programming language follows these principles:

    • declarations start with a keyword, so that the parser can be implemented with a single token look-ahead (like in Pascal)
    • the rest of the declaration follows the English grammar, with every redundant word left out (also like in Pascal, but with fewer keywords)

    Examples:

    • The type Frequency is a map indexed by string, mapping to int
      type Frequency map[string]int
    • The type Point is a struct with two fields, x and y of type int
      type Point struct { x, y int }

    The above sentences focus more on the names than on the types, which makes sense since the names convey more meaning.

    If I had to explain to novice programmers how to write declarations in Go, I would let them first describe it in plain English and then remove every word that might even seem redundant.

    Up to now, I didn't find any contradictions to these rules.

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

报告相同问题?

悬赏问题

  • ¥15 微信会员卡接入微信支付商户号收款
  • ¥15 如何获取烟草零售终端数据
  • ¥15 数学建模招标中位数问题
  • ¥15 phython路径名过长报错 不知道什么问题
  • ¥15 深度学习中模型转换该怎么实现
  • ¥15 HLs设计手写数字识别程序编译通不过
  • ¥15 Stata外部命令安装问题求帮助!
  • ¥15 从键盘随机输入A-H中的一串字符串,用七段数码管方法进行绘制。提交代码及运行截图。
  • ¥15 TYPCE母转母,插入认方向
  • ¥15 如何用python向钉钉机器人发送可以放大的图片?