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

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
主站蜘蛛池模板: 资溪县| 通榆县| 天等县| 华亭县| 鹤山市| 垣曲县| 湾仔区| 临邑县| 陇川县| 肃南| 永年县| 江西省| 石渠县| 永昌县| 开鲁县| 太和县| 寿宁县| 罗山县| 霞浦县| 湖北省| 柯坪县| 垣曲县| 抚宁县| 策勒县| 龙州县| 车险| 尼玛县| 酒泉市| 平和县| 葫芦岛市| 文安县| 榆林市| 江口县| 怀宁县| 富川| 武冈市| 阿尔山市| 平南县| 嘉禾县| 辽源市| 东山县|