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

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.
主站蜘蛛池模板: 麻江县| 日土县| 乌兰察布市| 玉屏| 平陆县| 阿拉尔市| 乐业县| 洪泽县| 宜良县| 寿光市| 白河县| 浦城县| 凉山| 武义县| 自贡市| 乾安县| 沂源县| 沙坪坝区| 新晃| 山东| 奉化市| 武邑县| 十堰市| 永平县| 灵寿县| 吴川市| 梁平县| 邛崃市| 绿春县| 安宁市| 晋宁县| 灵山县| 宜宾县| 资源县| 威信县| 宁强县| 扎囊县| 娄底市| 慈利县| 抚顺市| 芦山县|