Les fonctions
func printInfoNoParam() {
fmt.Printf("Name=%s, age=%d, email=%s\n", "Bob", 10, "bob@golang.org")
}
func printInfoParams(name string, age int, email string) {
fmt.Printf("Name=%s, age=%d, email=%s\n", name, age, email)
}
func avg(x, y float64) float64 {
return (x + y) / 2
}
func sumNamedReturn(x, y, z int) (sum int) {
sum = x + y + z
return // c pas bo hein ...
}
func main() {
printInfoNoParam()
printInfoParams("Noé", 15, "noe@flex.org")
result := avg(16.3, 25.0)
fmt.Printf("Average result=%f\n", result)
sum := sumNamedReturn(10, 25, 7)
fmt.Printf("Sum result=%d\n", sum)
}
Multiples return
func ToLowerStr(name string) (string, int) {
return strings.ToLower(name), len(name)
}
func main() {
lowerName, len := ToLowerStr("NOE") // on a le droit mais c'est pas beau non plus
fmt.Printf("%s (len=%d)\n", lowerName, len)
_, len = ToLowerStr("Paul ABIB, oui le seul le vrai l'unique")
fmt.Printf("bob len=%d\n", len)
}