1. 程式人生 > >The Zoo of Go Functions

The Zoo of Go Functions

Other Types

Recursive funcs

You can use recursive funcs as in any other langs, there is no real practical difference in Go. However, you must not forget that each call creates a new call stack. But, in Go, stacks are dynamic, they can shrink and grow depending on the needs of a func. If you can solve the problem at hand without a recursion prefer that instead.

Black hole funcs

A black hole func can be defined multiple times and they can’t be called in the usual ways. They’re sometimes useful to test a parser: see this.

func _() {}func _() {}

Inlined funcs

Go linker places a func into an executable to be able to call it later at the run-time. Sometimes calling a func is an expensive operation compared to executing the code directly. So, the compiler injects func’s body into the caller. To learn more about them: Read

this and this and this (by: Felipe) and this.

External funcs

If you omit the func’s body and only declare its signature, the linker will try to find it in an external func that may have written elsewhere. As an example, Atan func here just declared with a signature and then implemented in here.