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
+10.5k Golang : Check if an integer is negative or positive
+3.8k Unix/Linux : How to get own IP address ?
+3.1k Golang : ROT32768 (rotate by 0x80) UTF-8 strings example
+5.4k Golang : Find network service name from given port and protocol
+13k Golang : Fix cannot download, $GOPATH not set error
+5.8k Golang : Ordinal and Ordinalize a given number to the English ordinal numeral
+3.8k Golang : Find change in a combination of coins example
+7.7k Golang : Save webcamera frames to video file
+12.8k Golang : How to generate QR codes?
+6.3k Golang : How to control fmt or log print format?
+5.8k Golang : Implementing class(object-oriented programming style)
+14k Golang : Linked list example