Code with Go
About
  • Code With Go! 🚀
  • Hello World
  • Values
  • Variables
  • Constants
  • For Loop
  • If Else
  • Switch
  • Array
  • Slice
  • Map
  • Range
  • Functions
  • Multiple Return Values
  • Variadic Functions
  • Closure
  • Recursion
  • Pointers
  • Strings and Runes
  • Structs
  • Methods
  • Interfaces
  • Struct Embedding
  • Generics
  • Error
  • Goroutines
  • Channels
  • Channel Buffering
  • Channel Synchronization
  • Channel Directions
  • Select
  • Timeouts
  • Non-Blocking Channel Operations
  • Closing Channels
  • Range over Channels
  • Timers
  • Tickers
  • Worker Pools
  • Wait Groups
  • Rate Limiting
  • Atomic Counters
  • Mutexes
  • Stateful Goroutines
  • Sorting
  • Sorting by Functions
  • Panic
  • Defer
  • Recover
  • String Functions
  • String Formatting
  • Text Templates
  • Regular Expressions
  • JSON
  • XML
  • Time
  • Epoch
  • Time Formatting / Parsing
  • Random Numbers
  • Number Parsing
  • SHA256 Hashes
  • Base64 Encoding
  • Reading Files
  • Writing Files
  • Line Filters
  • File Paths
  • Directories
  • Temporary Files and Directories
  • Embed Directive
  • Testing and Benchmarking
  • Command-Line Arguments
  • Command-Line Flags
  • Command-Line Subcommands
  • Environment Variables
  • Logging
  • HTTP Client
  • HTTP Server
  • Context
  • Spawning Processes
  • Exec'ing Processes
  • Signals
  • Exit
  • Resize Image
Powered by GitBook
On this page

Was this helpful?

Edit on GitHub

Multiple Return Values

This Go program demonstrates the use of a function that returns multiple values.

// Importing the "fmt" package, which provides functions for formatted I/O.
import "fmt"

// Function vals returns two integers (3 and 7).
func vals() (int, int) {
    return 3, 7
}

// The main function, which serves as the entry point for the program.
func main() {
    // Calling the vals function and receiving two return values (a and b).
    a, b := vals()

    // Printing the value of variable 'a'.
    fmt.Println(a)

    // Printing the value of variable 'b'.
    fmt.Println(b)

    // Calling the vals function again, but using the blank identifier "_" to discard the first return value.
    // Only the second return value is assigned to variable 'c'.
    _, c := vals()

    // Printing the value of variable 'c'.
    fmt.Println(c)
}

Output

3
7
7

Now, let's break down the code and explain each part:

  1. Function Declaration:

    • vals() (int, int): This function returns two integers, 3 and 7.

  2. Main Function:

    • main(): This is the entry point of the program.

    • a, b := vals(): Calls the vals function and receives two return values (3 and 7), which are assigned to variables a and b.

    • fmt.Println(a): Prints the value of variable a.

    • fmt.Println(b): Prints the value of variable b.

    • _, c := vals(): Calls the vals function again, but uses the blank identifier "_" to discard the first return value. The second return value (7) is assigned to variable c.

    • fmt.Println(c): Prints the value of variable c.

PreviousFunctionsNextVariadic Functions

Last updated 1 year ago

Was this helpful?