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

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.
主站蜘蛛池模板: 新巴尔虎左旗| 哈尔滨市| 元谋县| 兴义市| 杨浦区| 上杭县| 大关县| 沁水县| 汝南县| 沙洋县| 揭东县| 大竹县| 乌审旗| 宜兰县| 社会| 三原县| 若羌县| 恭城| 辛集市| 崇礼县| 六枝特区| 贵阳市| 怀集县| 璧山县| 秦皇岛市| 贺兰县| 渝北区| 辉县市| 二手房| 当阳市| 蓬安县| 乌拉特前旗| 泾川县| 常德市| 营口市| 建宁县| 个旧市| 沛县| 古丈县| 饶河县| 大邑县|