This is a creation in Article, where the information may have evolved or changed. In the Go language, function is treated as a type that describes a set of functions that have the same signature and return value.
Since it is a type, we can manipulate the function type as a normal type, such as the following code fragment.
Package Mainimport ("FMT") type F func () type FS []ffunc main () {c: = Fs{func () {fmt. Print ("Hello")}, Func () {fmt. Println ("Douzifly")}}for I: = 0; I < Len (c); i++ {C[i] ()}}
The new type is defined by type f,f is actually the alias of the Func (), which represents a set of functions that have no return value.
Another new type is defined by type, which is a slice of f.
We easily put a set of functions inside a slice, and use loops to call the function inside the slice.
The output of the code snippet:
Hello Douzifly
To expand, we can define a map, map to a function by key, and we can invoke the function at run time through key.