Function dengan Return Multiple Value

Function dengan return multiple value memungkinkan output lebih dari 1 value.

Contoh code 1

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. ")
}
Hi Dawam nice to meet you. Welcome.

Contoh code 2 dengan mengabaikan 1 return value

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. ")
}
Hi Dawam nice to meet you.

Contoh code 3

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)
}
Area      : 600
Parameter: 100

Reference:

Last updated