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
+6.5k Default cipher that OpenSSL used to encrypt a PEM file
+21.3k SSL : How to check if current certificate is sha1 or sha2
+7.7k Golang : Sort words with first uppercase letter
+4.6k Golang : PGX CopyFrom to insert rows into Postgres database
+11.8k Golang : Split strings into command line arguments
+12.3k Golang : Add ASCII art to command line application launching process
+19.5k Golang : How to run your code only once with sync.Once object
+5.2k Golang : Stop goroutine without channel
+13.2k Golang : Read XML elements data with xml.CharData example
+7.2k Golang : Convert source code to assembly language
+39.1k Golang : Remove dashes(or any character) from string
+4.8k Golang : Experimental Jawi programming language