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

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.
主站蜘蛛池模板: 汪清县| 玉树县| 紫云| 宜宾县| 和顺县| 九江县| 巫山县| 大连市| 连云港市| 吴川市| 略阳县| 若羌县| 阳谷县| 庆城县| 沛县| 天等县| 道真| 阿拉善左旗| 中山市| 凤凰县| 徐水县| 新闻| 通道| 安图县| 崇礼县| 连州市| 宜城市| 句容市| 无为县| 山阴县| 恩平市| 密山市| 武平县| 建阳市| 闻喜县| 延川县| 罗源县| 略阳县| 镇沅| 江安县| 南通市|