I want to take an int slice as an input to a constructor and return a pointer to the original list, type casted into my external custom type(type IntList []int
).
I can do this:
type IntList []int
func NewIntListPtr(ints []int) *IntList {
x := IntList(ints)
return &x
}
But I cannot do this:
type IntList []int
func NewIntListPtr(ints []int) *IntList {
return &ints
}
// or this for that matter:
func NewIntListPtr(ints []int) *IntList {
return &(IntList(ints))
}
// or this
func NewIntListPtr(ints []int) *IntList {
return &IntList(*ints)
}
// or this
func NewIntListPtr(ints *[]int) *IntList {
return &(IntList(*ints))
}
Is there a one-liner to achieve the this?
You do it like this:
func NewIntListPtr(ints []int) *IntList {
return (*IntList)(&ints)
}