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

Go functions

Functions are an important element of every programming language because they allow you to break big programs into smaller and more manageable parts, but they must be as independent of each other as possible and must do one job and only one job. So, if you find yourself writing functions that do multiple things, you may want to consider writing multiple functions instead. However, Go will not refuse to compile functions that are long, complicated, or do multiple things.

A safe indication that you need to create a new function is when you find yourself using the same Go code multiple times in your program. Similarly, a safe indication that you need to put some of your functions in a module is when you find yourself using the same functions all the time in most of your programs.

The single most popular Go function is main(), which can be found in every autonomous Go program. If you look at the definition of the main() function, you'll soon realize that function declarations in Go start with the func keyword.

As a rule of thumb, you must try to write functions that are less than 20-30 lines of Go code. A good side effect of having smaller functions is that they can be optimized more easily because you can clearly find out where the bottleneck is.
主站蜘蛛池模板: 德庆县| 黑水县| 绵竹市| 株洲县| 涞源县| 万盛区| 福清市| 毕节市| 福清市| 吴川市| 长治市| 高安市| 大姚县| 夏河县| 辽源市| 金平| 乡城县| 玉树县| 巍山| 固阳县| 安福县| 会理县| 临沧市| 双柏县| 锦屏县| 东莞市| 子洲县| 南华县| 平塘县| 旅游| 拉萨市| 呼伦贝尔市| 广州市| 蓬溪县| 新干县| 宁城县| 江津市| 乌拉特后旗| 富川| 利津县| 开鲁县|