How to Pass Default Parameter to Golang Function

To pass the default parameter to a function in Golang, you can use a “variadic function”“struct with a default value”, or “map with a default value”.

Method 1: Using a variadic function

A variadic is a function that accepts a variable number of arguments. In Go, you can define a variadic function using the syntax before the last parameter type. This suggests that the function can accept any number of arguments of that type, which will be accessible within the function as a slice.

Example

package main

import (
  "fmt"
)

func Greet(name string, titles ...string) {
  title := "Mr."
  if len(titles) > 0 {
    title = titles[0]
  }
  fmt.Printf("Hello, %s %s!\n", title, name)
}

func main() {
  Greet("Krunal") // Uses the default title
  Greet("KB", "Ms.") // Passes a custom title
  Greet("Niva", "Dr.") // Passes a custom title
}

Output

Hello, Mr. Krunal!
Hello, Ms. KB!
Hello, Dr. Niva!

In this example, we defined a Greet() function with a name parameter and a variadic titles parameter.

Inside the function, we checked if any value was provided for titles. If a value is provided, we use the first value as the title; otherwise, we use the default value “Mr.”.

Method 2: Using a struct with a default value

You can also define a struct that holds the function parameters, including the ones you want to have default values. Then, in the function, check if the parameters have been set and assign the default values.

Example

package main

import (
  "fmt"
)

type GreetParams struct {
  Name string
  Title string
}

func GreetWithStruct(params GreetParams) {
  if params.Title == "" {
    params.Title = "Mr."
  }
  fmt.Printf("Hello, %s %s!\n", params.Title, params.Name)
}

func main() {
  GreetWithStruct(GreetParams{Name: "Krunal"})
  GreetWithStruct(GreetParams{Name: "KB", Title: "Ms."})
  GreetWithStruct(GreetParams{Name: "Niva", Title: "Dr."})
}

Output

In this example, we define a GreetParams struct to hold the parameters for the GreetWithStruct() function. The GreetWithStruct() function accepts an instance of GreetParams as its argument.

Inside the function, we checked if a value was provided for the Title field. If a value is provided, we use it; otherwise, we use the default value “Mr.”.

Method 3: Using a map with a default value

In Go, you can use a map to provide default values for function parameters. This can be helpful when you have a function that accepts many optional parameters.

Example

package main

import (
  "fmt"
)

func PrintDetails(details map[string]string) {
  defaults := map[string]string{
    "name": "Ankit",
    "age": "30",
    "country": "India",
  }

  for key, defaultValue := range defaults {
    if value, ok := details[key]; ok {
      fmt.Printf("%s: %s\n", key, value)
    } else {
      fmt.Printf("%s: %s\n", key, defaultValue)
    }
  }
}

func main() {
  PrintDetails(map[string]string{
    "name": "Krunal",
    "age": "30",
  })
}

Output

country: India
name: Krunal
age: 30

The PrintDetails() function then checks if the details map has a value for each key. If it does, it uses the value from the details map; otherwise, it uses the default value.

In the main function, we call PrintDetails() with a map that only has values for the “name” and “age” keys. The function will use these provided values for “name” and “age”, and the default value for “country”.