« Back to Index

[Golang Reduce Function Signature with Struct]

View original Gist on GitHub

Tags: #go #golang #struct #options #arguments #parameters #function #signature

Golang Reduce Function Signature with Struct.md

Instead of a long list of function arguments, consider using a struct.

Because types are initialized with a zero value, it means we can easily omit a field if we want.

type FooOptions struct { 
  beep string 
  boop string
  bing int
} 

f, err := foo(FooOptions{ 
  beep: "hello", 
  bing: 123, 
})

// notice `boop` field was omitted.