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.

Leave a Comment

How to Fix Microsoft Teams Error K Security

How to Fix Microsoft Teams Error K Security

Struggling with Microsoft Teams "Error K" Security? Follow our step-by-step guide to fix it quickly—no tech expertise needed. Get back to seamless collaboration today!

How to Fix Microsoft Teams Update Error 0x80070002

How to Fix Microsoft Teams Update Error 0x80070002

Struggling with Microsoft Teams Update Error 0x80070002? Discover step-by-step fixes to resolve it quickly. Clear cache, repair files, and get back to seamless updates today!

Solving Microsoft Teams Web Error Browser Login

Solving Microsoft Teams Web Error Browser Login

Tired of Microsoft Teams "Web Error" blocking your browser login? Follow our step-by-step guide with proven fixes to resolve Teams web login issues fast and securely. Get back to work!

How to Fix Microsoft Teams Error O Offline

How to Fix Microsoft Teams Error O Offline

Stuck with Microsoft Teams "Error O" Offline? Discover proven, step-by-step fixes to get back online fast. Clear cache, restart, and more – no tech skills needed!

Where is Microsoft Teams in Outlook? Locating the Missing Icon

Where is Microsoft Teams in Outlook? Locating the Missing Icon

Frustrated by the missing Microsoft Teams icon in Outlook? Learn exactly where to find it, why it disappears, and proven steps to restore it for effortless meetings. Updated for the latest versions!

Solving Microsoft Teams Windows 10 Error Login Issues

Solving Microsoft Teams Windows 10 Error Login Issues

Struggling with Microsoft Teams "Windows 10 Error" login issues? Get instant fixes for cache clears, updates, and more. Step-by-step solutions to solve Microsoft Teams login error on Windows 10 fast and frustration-free.

Solving Microsoft Teams Website Error Tab Not Loading

Solving Microsoft Teams Website Error Tab Not Loading

Tired of the frustrating Microsoft Teams "Website Error" where tabs won’t load? Get step-by-step fixes to resolve it quickly and boost your productivity. Essential troubleshooting for seamless Teams experience.

How to Fix Microsoft Teams Web Error 503 Service Unavailable

How to Fix Microsoft Teams Web Error 503 Service Unavailable

Tired of the frustrating Microsoft Teams "Web Error" 503 Service Unavailable? Discover proven, step-by-step fixes for Teams 503 error on web. Get back online fast with our expert guide. Works on all browsers!

How to Fix Microsoft Teams Win 7 Error Compatibility

How to Fix Microsoft Teams Win 7 Error Compatibility

Struggling with Microsoft Teams "Win 7 Error" compatibility? Discover step-by-step fixes to restore seamless video calls and chats on unsupported Windows versions. Quick, reliable solutions inside!

Troubleshooting Microsoft Teams Breakout Rooms License Errors

Troubleshooting Microsoft Teams Breakout Rooms License Errors

Master troubleshooting Microsoft Teams Breakout Rooms license errors with this step-by-step guide. Quick fixes for common license issues, admin checks, and prevention tips to get your meetings running smoothly.

How to Fix Microsoft Teams Version History Error

How to Fix Microsoft Teams Version History Error

Struggling with Microsoft Teams "Version History" Error? Discover proven, step-by-step fixes to restore access instantly. Clear cache, update Teams, and more—no tech skills needed!

Solving Microsoft Teams Joining Error: Meeting ID Not Found

Solving Microsoft Teams Joining Error: Meeting ID Not Found

Tired of the frustrating Microsoft Teams Joining Error: Meeting ID Not Found? Get step-by-step fixes to rejoin meetings fast. Updated with the latest Teams patches for seamless collaboration. Solve it now!

Troubleshooting Microsoft Teams Video Error Green Screen

Troubleshooting Microsoft Teams Video Error Green Screen

Struggling with Microsoft Teams "Video Error" green screen? Discover proven, step-by-step troubleshooting fixes for seamless video calls. Quick solutions inside!

How to Fix Microsoft Teams How Teams Works Tutorial Error

How to Fix Microsoft Teams How Teams Works Tutorial Error

Struggling with the Microsoft Teams "How Teams Works" Tutorial Error? Discover proven, step-by-step fixes to resolve it quickly. Clear cache, update, and more for seamless onboarding. Works on latest versions!

How to Fix Microsoft Teams Error Today on Windows 10

How to Fix Microsoft Teams Error Today on Windows 10

Tired of the frustrating Microsoft Teams "Error Today" on Windows 10? Discover proven, step-by-step fixes to resolve it quickly and restore smooth teamwork. No tech skills needed!