duanjiushu5063 2017-06-23 10:42
浏览 30
已采纳

去样式指南? [关闭]

I'm starting in the programming with Go and I want to ask if there are some patterns to follow when programming, like:

// Package

// Structs orderer by importance

// Struct methods

// Un-exported methods

// Exported methods

// getters and setters

  • 写回答

2条回答 默认 最新

  • douceng7070 2017-06-23 11:00
    关注

    There is no specific guideline for this and even stdlib packages does not follow the same way. But as a rule of thumb you should:

    • Put exported, global constants and variables (like errors) near the top.
    • Keep structs together with their methods rather than grouping structs together and then their methods separately.
    • Group logical parts together (whenever structs, their methods, package functions (exported or not)). It will make easier splitting your package into multiple files later on. It makes reading easier as well as you don't have to jump all over the file.
    • Last, but not least: if you generate documentation from your package with godoc and can read it from top to bottom with understanding, then your order is probably correct :)

    When in doubt, check some popular stdlib packages, like https://golang.org/src/net/http/server.go.

    Other useful code style guidelines can be found here:

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

报告相同问题?

悬赏问题

  • ¥15 安装svn网络有问题怎么办
  • ¥15 Python爬取指定微博话题下的内容,保存为txt
  • ¥15 vue2登录调用后端接口如何实现
  • ¥65 永磁型步进电机PID算法
  • ¥15 sqlite 附加(attach database)加密数据库时,返回26是什么原因呢?
  • ¥88 找成都本地经验丰富懂小程序开发的技术大咖
  • ¥15 如何处理复杂数据表格的除法运算
  • ¥15 如何用stc8h1k08的片子做485数据透传的功能?(关键词-串口)
  • ¥15 有兄弟姐妹会用word插图功能制作类似citespace的图片吗?
  • ¥15 latex怎么处理论文引理引用参考文献