create a function with multiple parameters in go

To create a function with multiple parameters in Go, you need to declare the function with the parameter names and types within the parentheses following the function name.

Here's an example of a function with three parameters:

main.go
func addNumbers(num1 int, num2 int, num3 int) int {
    return num1 + num2 + num3
}
84 chars
4 lines

In this example, the function is named addNumbers and takes three parameters of type int. The function returns the sum of the three input numbers.

Alternatively, you can also declare multiple parameters of the same type using the following syntax:

main.go
func multiplyNumbers(num1, num2, num3 int) int {
    return num1 * num2 * num3
}
81 chars
4 lines

In this example, the multiplyNumbers function also takes three parameters but all of them are of type int. The function returns the product of the three input numbers.

Both functions can be called like this:

main.go
result := addNumbers(2, 3, 5)
result2 := multiplyNumbers(2, 3, 5)
66 chars
3 lines

gistlibby LogSnag