官术网_书友最值得收藏!

Avoiding common Go mistakes

This section will briefly talk about some common Go mistakes so that you can avoid them in your programs:

  • If you have an error in a Go function, either log it or return it; do not do both unless you have a really good reason to do so.
  • Go interfaces define behaviors, not data and data structures.
  • Use the io.Reader and io.Writer interfaces because they make your code more extensible.
  • Make sure that you pass a pointer to a variable to a function only when needed. The rest of the time, just pass the value of the variable.
  • Error variables are not strings; they are error values.
  • If you are afraid of making mistakes, you will most likely end up doing nothing useful. So experiment as much as you can.

The following are general pieces of advice that can be applied in every programming language:

  • Test your Go code and functions in small and autonomous Go programs to make sure that they behave the way you think they should
  • If you do not really know a Go feature, test it before using it for the first time, especially if you are developing a systems utility
  • Do not test systems software on production machines
  • When you deploy your systems software on a production machine, do it when the production machine is not busy and make sure that you have a backup plan
主站蜘蛛池模板: 泽库县| 易门县| 抚顺市| 桦川县| 宜兴市| 绍兴县| 东乌珠穆沁旗| 米泉市| 庄浪县| 华池县| 吉水县| 建平县| 客服| 阳谷县| 台中市| 芜湖市| 南部县| 宜兰市| 宿迁市| 郯城县| 绵阳市| 西安市| 瓦房店市| 宁波市| 新泰市| 民权县| 岐山县| 邛崃市| 化州市| 靖安县| 克山县| 阳高县| 修文县| 肥东县| 凌源市| 新竹市| 武安市| 牟定县| 通许县| 南川市| 靖安县|