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

Variadic Functions

This Go program demonstrates the use of variadic functions and the spread operator.

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

// Function sum takes a variable number of integers as parameters using the ellipsis (...) syntax.
// It prints the input numbers and calculates their sum.
func sum(nums ...int) {
    // Print the input numbers.
    fmt.Print(nums, " ")

    // Initialize a variable to store the sum.
    total := 0

    // Iterate over the input numbers and calculate the sum.
    for _, num := range nums {
        total += num
    }

    // Print the calculated sum.
    fmt.Println(total)
}

// The main function, which serves as the entry point for the program.
func main() {
    // Calling the sum function with two integer arguments (1 and 2).
    sum(1, 2)

    // Calling the sum function with three integer arguments (1, 2, and 3).
    sum(1, 2, 3)

    // Creating a slice of integers and passing it to the sum function using the spread operator (...).
    nums := []int{1, 2, 3, 4}
    sum(nums...)
}

Output

[1 2] 3
[1 2 3] 6
[1 2 3 4] 10

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

  1. Function Declarations:

    • sum(nums ...int): This function takes a variable number of integers using the ellipsis (...) syntax. It prints the input numbers and calculates their sum.

  2. Main Function:

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

    • sum(1, 2): Calls the sum function with two integer arguments (1 and 2).

    • sum(1, 2, 3): Calls the sum function with three integer arguments (1, 2, and 3).

    • nums := []int{1, 2, 3, 4}: Creates a slice of integers.

    • sum(nums...): Calls the sum function with the elements of the nums slice passed as individual arguments using the spread operator ....

PreviousMultiple Return ValuesNextClosure

Last updated 1 year ago

Was this helpful?