dp13668681869 2018-10-16 09:38
浏览 295

主包中struct类型的名字的首字母的大小写

So it should be lowercase if it's not imported. And that's strange to import something from a main package. But, that's look very strange for me to use lowercase for the first of a struct/class from experience with other languages. What way is idiomatic?

  • 写回答

1条回答 默认 最新

  • duanhuancong1969 2018-10-16 10:04
    关注

    This isn't a question of what's idiomatic, if an identifier starts with a lowercase letter, it's not exported and therefore can only be accessed from the declaring package (directly), and if an identifier starts with an uppercase letter (Unicode class "Lu"), it's exported and can be referred to from any package. For details, see Spec: Exported identifiers. This was a design decision, which I personally like (it keeps the source clean, and "They even have semantic effect: the visibility of a name outside a package is determined by whether its first character is upper case"Effective Go: Names).

    The main package is special: you can't import the main package, so it doesn't matter how you name your identifiers in the main package, no one (no package other than the main) will refer to them / use them.

    See more releavant blog posts:

    Go FAQ: What's up with Unicode identifiers?

    Ardan labs: Exported / Unexported Identifiers In Go

    评论

报告相同问题?

悬赏问题

  • ¥20 数学建模,尽量用matlab回答,论文格式
  • ¥15 昨天挂载了一下u盘,然后拔了
  • ¥30 win from 窗口最大最小化,控件放大缩小,闪烁问题
  • ¥20 易康econgnition精度验证
  • ¥15 msix packaging tool打包问题
  • ¥28 微信小程序开发页面布局没问题,真机调试的时候页面布局就乱了
  • ¥15 python的qt5界面
  • ¥15 无线电能传输系统MATLAB仿真问题
  • ¥50 如何用脚本实现输入法的热键设置
  • ¥20 我想使用一些网络协议或者部分协议也行,主要想实现类似于traceroute的一定步长内的路由拓扑功能