5 ms·
You could always use a pointer for this, but admittedly it is pretty ugly when compared to a true optional type: func f(s *string) string { ret :=
by vishvananda 5y ago
You could always use a pointer for this, but admittedly it is pretty ugly when compared to a true optional type:
func f(s *string) string {
ret := ""
if s == nil {
ret = "default"
} else {
ret = *s
}
return ret
}
func main() {
s := "foo"
fmt.Println("Hello, " + f(&s))
fmt.Println("Hello, " + f(nil))
}