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

The best laptops for students in 2025

The best laptops for students in 2025

Students need a specific type of laptop for their studies. It should not only be powerful enough to perform well in their chosen major, but also compact and light enough to carry around all day.

Ways to reduce the risk of birth defects in the fetus

Ways to reduce the risk of birth defects in the fetus

Birth defects are something no one wants. Although they cannot be completely prevented, you can take the following steps to reduce the risk of birth defects in your baby.

How to check RAM and check RAM errors on your computer with the highest accuracy rate

How to check RAM and check RAM errors on your computer with the highest accuracy rate

As you know, RAM is a very important hardware part in a computer, acting as memory to process data and is the factor that determines the speed of a laptop or PC. In the article below, WebTech360 will introduce you to some ways to check for RAM errors using software on Windows.

Top 5 best automatic home coffee makers

Top 5 best automatic home coffee makers

The automatic home coffee maker is a modern and professional product, bringing you and your family delicious cups of coffee with just a few quick steps.

Difference between regular TV and Smart TV

Difference between regular TV and Smart TV

Smart TVs have really taken the world by storm. With so many great features and the ability to connect to the Internet, technology has changed the way we watch TV.

Why doesnt the freezer have a light but the refrigerator does?

Why doesnt the freezer have a light but the refrigerator does?

Refrigerators are familiar appliances in families. Refrigerators usually have 2 compartments, the cool compartment is spacious and has a light that automatically turns on every time the user opens it, while the freezer compartment is narrow and has no light.

2 Ways to Fix Network Congestion That Slows Down Wi-Fi

2 Ways to Fix Network Congestion That Slows Down Wi-Fi

Wi-Fi networks are affected by many factors beyond routers, bandwidth, and interference, but there are some smart ways to boost your network.

How to Downgrade from iOS 17 to iOS 16 without Losing Data using Tenorshare Reiboot

How to Downgrade from iOS 17 to iOS 16 without Losing Data using Tenorshare Reiboot

If you want to go back to stable iOS 16 on your phone, here is the basic guide to uninstall iOS 17 and downgrade from iOS 17 to 16.

What happens to the body when you eat yogurt every day?

What happens to the body when you eat yogurt every day?

Yogurt is a great food. Is it good to eat yogurt every day? What will happen to your body when you eat yogurt every day? Let's find out together!

Which type of rice is best for health?

Which type of rice is best for health?

This article discusses the most nutritious types of rice and how to maximize the health benefits of whichever rice you choose.

How to wake up on time in the morning

How to wake up on time in the morning

Establishing a sleep schedule and bedtime routine, changing your alarm clock, and adjusting your diet are some of the measures that can help you sleep better and wake up on time in the morning.

Rent Please! Landlord Sim Tips for Beginners

Rent Please! Landlord Sim Tips for Beginners

Rent Please! Landlord Sim is a simulation mobile game on iOS and Android. You will play as a landlord of an apartment complex and start renting out an apartment with the goal of upgrading the interior of your apartments and getting them ready for rent.

Latest Bathroom Tower Defense Codes and How to Enter Codes

Latest Bathroom Tower Defense Codes and How to Enter Codes

Get Bathroom Tower Defense Roblox game codes and redeem them for exciting rewards. They will help you upgrade or unlock towers with higher damage.

Structure, symbols and operating principles of transformers

Structure, symbols and operating principles of transformers

Let's learn about the structure, symbols and operating principles of transformers in the most accurate way.

4 Ways AI Is Making Smart TVs Better

4 Ways AI Is Making Smart TVs Better

From better picture and sound quality to voice control and more, these AI-powered features are making smart TVs so much better!