Hi I want to log the value of a variable if it is not nil otherwise I want to print anything else
for example:
var point *string
var point2 *string
p:="hi"
point2=&p
fmt.Printf("%v,%v",*point,*point2)
in this case I will have an error since point is nill, so is there any way to print the value of the variable if it is not nil or print anything else if it is nil?
I want to do this in a simple way instead of creating an if/else statement to check if the variable is nil
Since there is no ?:
operator in Go, the best way is to write a function:
func StringPtrToString(p *string) string {
if p != nil { return *p }
return "(nil)"
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With