Golang : How to check for empty array string or string?
Sometimes that most obvious thing can be the hardest to remember. Putting this down as a note for myself and maybe useful to you too.
Problem :
Need to check if a string or []string is empty. How to do that?
Solutions :
if str is a string
len(strings.TrimSpace(str)) == 0
and let say str is an array []string
var str []string
strings.TrimSpace(strings.Join(str, "")) == ""
Reference :
https://groups.google.com/forum/#!topic/golang-nuts/7Ks1iq2s7FA
By Adam Ng
IF you gain some knowledge or the information here solved your programming problem. Please consider donating to the less fortunate or some charities that you like. Apart from donation, planting trees, volunteering or reducing your carbon footprint will be great too.
Advertisement
Tutorials
+9.6k Golang : Detect number of active displays and the display's resolution
+6.4k Golang : Combine slices of complex numbers and operation example
+8.8k Golang : Populate or initialize struct with values example
+40.2k Golang : Convert to io.ReadSeeker type
+5.3k Golang : Pad file extension automagically
+32.9k Delete a directory in Go
+9.2k Golang : Temperatures conversion example
+9.7k Golang : ffmpeg with os/exec.Command() returns non-zero status
+7.7k Golang : Regular Expression find string example
+6k Java : Human readable password generator
+9.3k Golang : Play .WAV file from command line
+16.9k Golang : How to save log messages to file?