
As in almost all languages, there is a way to encapsulate a set of instructions that performs a specific processing, it is the black box principle, with a set of input parameters and an output result. With the functions one abstracts from the detail of the implementation. It could be written by another person for example.
The term _routine_was also used.
The functions also make it possible to split and identify the different treatments.
func <FunctionName>(<Param1> type, <Param2> type, ...) <TypeRetour> { // Implémentation }
The most glaring example is the main function of any GO program for the point of entry.
package main import ( "fmt" ) func main() { var resultat int = 1 var monTexte string = "Resultat: " + resultat fmt.Printf("%vn", monTexte) }
Important: In terms of the visibility of a function, in this sense we are talking about public or private. This is done by either putting the first letter in lowercase for a private function and a capital letter for a public service.
It is all well and good to have declared a function. It is necessary to call on its services. The example below is voluntarily detailed. This function takes an input parameter and returns an output parameter.
package main import "fmt" func main() { var name string = "John Doe" messageForYou := message(name) fmt.Println(messageForYou) } func message(nom string) string { return fmt.Sprintf("Hello dear %s.", nom) } messageForYou := message(name)
In summary
And now you know:
Declare functions with input and/or output parameters;
Change the scope of the function in private or public which will be highly useful when writing packages for example;
Make the call has this function and retrieve its result.