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
+7.9k Golang : HTTP Server Example
+11.7k Golang : Find and draw contours with OpenCV example
+15.4k Golang : Convert date format and separator yyyy-mm-dd to dd-mm-yyyy
+7.9k Golang : Randomize letters from a string example
+4.4k Mac OSX : Get disk partitions' size, type and name
+18.2k Golang : Logging with logrus
+9.9k Golang : Compare files modify date example
+9.7k Golang : Sort and reverse sort a slice of integers
+21.9k Golang : Print leading(padding) zero or spaces in fmt.Printf?
+48.2k Golang : Upload file from web browser to server
+25.7k Mac/Linux and Golang : Fix bind: address already in use error
+5.5k List of Golang XML tutorials