DEV Community

More easily leveraging bool's from a ptr

Nick Huanca on June 14, 2019

I am currently working on a project that leverages pointers to Bool values in golang. I am using corev1.SecurityContext.RunAsNonRoot which is a *bo...
Collapse
 
maxatome profile image
Maxime Soulé

You can use this code:

isItTrue := boolPtr != nil && *boolPtr

or use a helper function, as it will probably be inlined by the compiler.
Note that your isNilbool function is useless. Using a function to do a simple == is overkill.

Collapse
 
nickhuanca profile image
Nick Huanca

thanks for the helpful feedback Maxime - I'll definitely adjust my approach :)