Function dengan return multiple value memungkinkan output lebih dari 1 value.
Copy package main
import (
"fmt"
)
func main () {
result1, result2 := sayHi ( "Dawam" )
fmt. Println (result1, result2)
}
func sayHi (name string ) ( string , string ) {
return fmt. Sprintf ( "Hi %s nice to meet you." , name), fmt. Sprintf ( "Welcome. " )
}
Copy Hi Dawam nice to meet you. Welcome.
Contoh code 2 dengan mengabaikan 1 return value
Copy package main
import (
"fmt"
)
func main () {
result1, _ := sayHi ( "Dawam" )
fmt. Println (result1)
}
func sayHi (name string ) ( string , string ) {
return fmt. Sprintf ( "Hi %s nice to meet you." , name), fmt. Sprintf ( "Welcome. " )
}
Copy Hi Dawam nice to meet you.
Copy package main
import "fmt"
func RectangleFormula (p, l int ) (area int , parameter int ) {
return p * l, 2 * (p + l)
}
func main () {
a, p := RectangleFormula ( 20 , 30 )
fmt. Println ( "Area :" , a)
fmt. Println ( "Parameter:" , p)
}
Copy Area : 600
Parameter: 100