Volatile functions in Golang

Variadic functions in Go allow you to pass a variable number of arguments to a function. This is useful when you don't know in advance how many arguments you will pass. A variadic function in Golang  accepts multiple arguments of the same type and can be called with any number of arguments, including none.

Volatile functions in Golang

package main
import "fmt"

// Hàm Variadic tính tổng
func sum(nums ...int) int {
    total := 0
    for _, n := range nums {
        total += n
    }
    return total
}

func main() {
    fmt.Println("Sum of 1, 2, 3:", sum(1, 2, 3))
    fmt.Println("Sum of 4, 5:", sum(4, 5))
    fmt.Println("Sum of no numbers:", sum())
}

Result:

Sum of 1, 2, 3: 6
Sum of 4, 5: 9
Sum of no numbers: 0

Syntax:

func functionName(parameters ...Type) ReturnType {
    // Code
}

In the above syntax:

  • parameters ...Typeindicates that the function can accept a variable number of arguments of type Type.
  • You can access arguments in a function as a slice.

How to use uncertain functions in Golang

Using Variadic functions

When defining a variadic function, you specify the argument types followed by an ellipsis (...) as in the example above. Inside the function, these arguments can be thought of as a slice.

Calling a variadic function

You can call a variadic function with any number of arguments, including zero. The function treats the arguments as a slice.

For example:

package main
import "fmt"

func sum(nums ...int) int {
    total := 0
    for _, n := range nums {
        total += n
    }
    return total
}

func main() {
    fmt.Println("Sum of 1, 2, 3:", sum(1, 2, 3))
    fmt.Println("Sum of 4, 5:", sum(4, 5))
    fmt.Println("Sum of no numbers:", sum())
}

Result:

Sum of 1, 2, 3: 6
Sum of 4, 5: 9
Sum of no numbers: 0

Variadic function with different parameters

You can also combine variadic parameters with regular parameters in a function. Variadic parameters must always be the last parameter.

For example:

package main
import "fmt"

// Hàm Variadic tính tổng
func sum(nums ...int) int {
    total := 0
    for _, n := range nums {
        total += n
    }
    return total
}

// Hàm với tham số thông thường và variadic 
func greet(prefix string, nums ...int) {
    fmt.Println(prefix)
    for _, n := range nums {
        fmt.Println("Number:", n)
    }
}
func main() {
    greet("Sum of numbers:", 1, 2, 3)
    greet("Another sum:", 4, 5)
    greet("No numbers sum:")
}

Result:

Sum of numbers:
Number: 1
Number: 2
Number: 3
Another sum:
Number: 4
Number: 5
No numbers sum:

Limitations of Variadic functions

  • Variadic functions can have only one variadic parameter and it must be the last parameter.
  • You cannot have multiple variadic parameters in a single function definition.
Sign up and earn $1000 a day ⋙

Leave a Comment

What is a mixed number?

What is a mixed number?

A mixed number is a combination of a whole number and a fraction. The fractional part of a mixed number is always less than 1.

Tips for traveling like a rich person

Tips for traveling like a rich person

Are you on a budget when it comes to traveling? Don’t worry, here are some tips from a famous travel advisor that will help you travel like a rich person.

7 Best Obsidian Plugins for Mobile

7 Best Obsidian Plugins for Mobile

In addition to tweaking the interface and making Obsidian run more efficiently, they'll give you a richer set of note-taking tools.

How to fix a blurry external display

How to fix a blurry external display

A dim external monitor can disrupt productivity and cause eye strain. Luckily, there are plenty of things you can do to fix a dim display!

Where do the symbols of male and female come from?

Where do the symbols of male and female come from?

The meaning of popular male and female symbols today may not be known to everyone. Let's learn more about male and female symbols!

The mysterious history of black holes has been decoded by scientists

The mysterious history of black holes has been decoded by scientists

One of the common misconceptions about black holes is that they not only swallow matter, but also the history of that matter. The truth about the history of black holes in the universe has finally been revealed.

No need for multiple Adobe Mobile apps, Adobe Express is the only app you need!

No need for multiple Adobe Mobile apps, Adobe Express is the only app you need!

Adobe offers more than 4 specific photo editing apps on the phone - all of which are so similar, it's hard to decipher which one you really need.

5 gadgets that turn a regular TV into a smart one

5 gadgets that turn a regular TV into a smart one

A regular TV can still learn new tricks with a few gadgets and become better than an expensive smart TV in minutes.

How to use Panasonic air conditioner remote control

How to use Panasonic air conditioner remote control

Using Panasonic air conditioner remote control properly will help us take advantage of the features on the air conditioner.

Gemini rolls out Talk Live about for photos, files, and YouTube

Gemini rolls out Talk Live about for photos, files, and YouTube

Gemini Live has added a “Talk Live about” feature, and it’s now rolling out more widely to Android devices. Previously, Gemini Live only accepted voice input, but “Talk Live about” has expanded the content uploads

Microsoft Copilot Upgraded with Deep Research

Microsoft Copilot Upgraded with Deep Research

Microsoft is finally bringing Deep Research functionality to its Copilot AI. It allows users to conduct in-depth, multi-step research on any topic.

How to Turn a Photo into a Pencil Sketch in Photoshop

How to Turn a Photo into a Pencil Sketch in Photoshop

This tutorial will show you how to easily turn a photo into a pencil sketch using Adobe Photoshop.

Volatile functions in Golang

Volatile functions in Golang

Variadic functions in Go allow you to pass a variable number of arguments to a function. Here's everything you need to know about variadic functions in Golang.

How to write color letters in Lien Quan Mobile

How to write color letters in Lien Quan Mobile

To write colored letters in Lien Quan Mobile, follow the instructions in this article. Colored letters in LQ Mobile will attract more attention.

7 Task Manager Processes That Should Never End

7 Task Manager Processes That Should Never End

The Windows system processes section, located at the bottom of the list in Windows 10 Task Manager, contains several important processes that are essential for your computer to run properly.