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
+8.7k Golang : Combine slices but preserve order example
+20.7k Golang : Saving private and public key to files
+14k Golang : Fix cannot use buffer (type bytes.Buffer) as type io.Writer(Write method has pointer receiver) error
+12.7k Golang : zlib compress file example
+15.3k nginx: [emerg] unknown directive "ssl"
+5.8k Cash Flow : 50 days to pay your credit card debt
+15.9k Golang : Read a file line by line
+33.9k Golang : Call a function after some delay(time.Sleep and Tick)
+18.1k Golang : Convert IPv4 address to decimal number(base 10) or integer
+30.8k Golang : Download file example
+20.3k Golang : Check if os.Stdin input data is piped or from terminal
+14.3k Golang : How to shuffle elements in array or slice?