Search
 
SCRIPT & CODE EXAMPLE
 

C

go optional parameters

// **Go doesn't support optional parameters natively.**
// Yeah, it is frustrating!
// Link to discussion: https://stackoverflow.com/questions/2032149/optional-parameters-in-go
// * This is a simple hack based on @ferguzz's answer: https://stackoverflow.com/a/19813113/13791656:

package main

import (
  "fmt"
)
  
type CustomOptions struct {
	option1 int
    option2 string
	option3 bool
}

func foo(requiredParam string, optionalParams ...CustomOptions) {
    fmt.Println(len(optionalParams)) // returns how many Custom Options are included
    option1 = optionalParams[0].option1 // params is an array
	fmt.print(optionalParams[0].option1)
}

func main() {
    foo("I'm required")
    foo("I'm required", CustomOptions(option1: 1))
    foo("I'm required", CustomOptions(option1: 1, option2: "2", option3: true))
}
Comment

PREVIOUS NEXT
Code Example
C :: how to modulo in c without use the operator 
C :: Area of a Circle in C Programming 
C :: c if else 
C :: odd even in c with ternary operator 
C :: Access denied creating xampp-control.ini 
C :: go Iterating over an array using a range operator 
C :: Gemfile.lock`. It is likely that you need to grant write permissions for that path. 
C :: input array elements in c 
C :: ruby find object in array by attribute 
C :: stack push code 
C :: plt legend top right outside 
C :: Grepper VSCode Add On 
C :: Example of Implementation of a pointer to an array in C: 
C :: Passing a matrix in a function C 
C :: C (ANSI) 
C :: print command for rust unit-test 
C :: c remove last charachter from string 
C :: print float in c 
C :: maximo comun divisor 
C :: C program to input the month number and output the month name using switch statement 
C :: mediawiki upload size 
C :: C program to find power of any number 
C :: get boolean from localstorage 
C :: compile in c 
C :: Example of read and write project in c 
C :: C Pass Individual Array Elements 
C :: how to print chicken in c 
C :: convert c code to python online free 
C :: bool print variable in c 
C :: extended euclidean algorithm to find x and y 
ADD CONTENT
Topic
Content
Source link
Name
2+3 =