Follow

Follow

Optional Parameters in GO lang!

Maheshwar Ligade's photo
Maheshwar Ligade
·May 10, 2023·

2 min read

Play this article

Table of contents

  • Option 1: Variadic parameters
  • Option 2: Using pointers
  • Option 3: Using structs
  • Conclusion

Introduction

Optional parameters allow functions to have parameters that are not mandatory. In Go, there is no built-in support for optional parameters, but there are several ways to achieve this functionality.

Option 1: Variadic parameters

One way to simulate optional parameters is to use variadic parameters. Variadic parameters allow a function to accept a variable number of arguments. By using variadic parameters, we can specify optional parameters that can be omitted when calling the function.

Example:

func exampleFunction(requiredParam string, optionalParams ...string) {
    // code
}

In the above example, requiredParam is a required parameter, while optionalParams is a variadic parameter that can accept zero or more additional arguments.

Option 2: Using pointers

Another way to simulate optional parameters is to use pointers. We can declare a pointer-type parameter and initialize it to nil. By checking if the parameter is nil, we can determine if the parameter was omitted.

Example:

func exampleFunction(requiredParam string, optionalParam *string) {
    if optionalParam == nil {
        // optional parameter was omitted
    } else {
        // optional parameter was included
    }
}

In the above example, optionalParam is a pointer-type parameter that can be nil. By checking if it is nil, we can determine if the parameter was omitted or else do the processing.

Option 3: Using structs

A third way to simulate optional parameters is to use structs. We can define a struct type that contains fields for all possible parameters, and then create a variable of that struct type with default values for the optional parameters. By only setting the values for the parameters that are required, we can simulate optional parameters.

Example:

type exampleParams struct {
    requiredParam string
    optionalParam1 string
    optionalParam2 string
}

func exampleFunction(params exampleParams) {
    // code
}

In the above example, params is a struct type that contains fields for all possible parameters. By only setting the values for the parameters that are required, we can simulate optional parameters.

Conclusion

While Go does not have built-in support for optional parameters, there are several ways to achieve this functionality. Variadic parameters, pointers, and structs can all be used to simulate optional parameters. The approach you choose will depend on the specific requirements of your code.

I hope this helps, you!!

More such articles:

https://medium.com/techwasti

https://www.youtube.com/channel/UCiTaHm1AYqMS4F4L9zyO7qA

https://www.techwasti.com/

\==========================**=========================

If this article adds any value to you then please clap and comment.

Let’s connect on Stackoverflow, LinkedIn, & Twitter.

Did you find this article valuable?

Support techwasti by becoming a sponsor. Any amount is appreciated!

See recent sponsors Learn more about Hashnode Sponsors
 
Share this