Type conversion in go
Type conversion in go looks a lot like function calls (in python too but here it confused me).
Following looks like Bind returns the result of OptionFunc function call. This function accepts a function as an argument, this is not a very uncommon approach.
func Bind(args ...any) Option {
return OptionFunc(func(k *Kong) error {
k.bindings.add(args...)
return nil
})
}Although, the definition of OptionFunc is:
// OptionFunc is function that adheres to the Option interface.
type OptionFunc func(k *Kong) errorOptionFunc is a type and the initial snippet was a type conversion not a function call !
It merely sets the type of the unnamed function func(k *Kong) ... to OptionFunc.
Following might jog memories about creating types from existing types:
type ENV string
const (
"dev" ENV
"release" ENV
)
Read other posts