Multiple Return Values
This Go program demonstrates the use of a function that returns multiple values.
Output
Now, let's break down the code and explain each part:
Function Declaration:
vals() (int, int)
: This function returns two integers, 3 and 7.
Main Function:
main()
: This is the entry point of the program.a, b := vals()
: Calls thevals
function and receives two return values (3 and 7), which are assigned to variablesa
andb
.fmt.Println(a)
: Prints the value of variablea
.fmt.Println(b)
: Prints the value of variableb
._, c := vals()
: Calls thevals
function again, but uses the blank identifier "_" to discard the first return value. The second return value (7) is assigned to variablec
.fmt.Println(c)
: Prints the value of variablec
.
Last updated
Was this helpful?