dttphb59319 2016-11-30 06:49
浏览 29
已采纳

类似Golang变量的命名约定

I have a couple of cases where I have the same underlying value being represented as multiple types.

Example :

userIDString := r.URL.Query("id")
userID, err :=  strconv.Atoi(userIDString)

I need to use both the above variables at different places.

Similarly

recordSeparator = rune(30)
recordSeparatorString = string(30)

Is my approach to naming such variables considered idiomatic go ? If not what would be the ideal naming convention for such cases ?

PS: I don't think this question is primarily opinion based, I'm looking for answers referencing the naming conventions in popular go projects / standard lib.

  • 写回答

1条回答 默认 最新

  • douduiwei2831 2016-11-30 09:38
    关注

    The likely most authoritative book in the field, The Go Programming Language, discusses this topic in section 10.6 Packages and Naming:

    • keep names short but don't make them cryptic (user over userName)
    • package names usually take singular form (unless there's a conflict with predeclared types)
    • pick names so that they read in the context of the package, for example, net.IP

    In addition, there's a nice slide deck What's in a name addressing some of the questions and a somewhat informative reddit thread that might be useful as well.

    Most of the naming conventions in my experience (in addition to the above mentioned) are however project or company specific.

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

报告相同问题?

悬赏问题

  • ¥15 关于#hadoop#的问题
  • ¥15 (标签-Python|关键词-socket)
  • ¥15 keil里为什么main.c定义的函数在it.c调用不了
  • ¥50 切换TabTip键盘的输入法
  • ¥15 可否在不同线程中调用封装数据库操作的类
  • ¥15 微带串馈天线阵列每个阵元宽度计算
  • ¥15 keil的map文件中Image component sizes各项意思
  • ¥20 求个正点原子stm32f407开发版的贪吃蛇游戏
  • ¥15 划分vlan后,链路不通了?
  • ¥20 求各位懂行的人,注册表能不能看到usb使用得具体信息,干了什么,传输了什么数据