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.4k Golang : How to get username from email address
+7.7k Golang : get the current working directory of a running program
+21.7k Golang : Setting up/configure AWS credentials with official aws-sdk-go
+4k Detect if Google Analytics and Developer Media are loaded properly or not
+9k Golang : Get SPF and DMARC from email headers to fight spam
+8.6k Golang : Add text to image and get OpenCV's X, Y co-ordinates example
+15.3k nginx: [emerg] unknown directive "ssl"
+23.5k Golang : Check if element exist in map
+11.6k SSL : The certificate is not trusted because no issuer chain was provided
+28.6k Get file path of temporary file in Go
+27.7k PHP : Count number of JSON items/objects
+6.1k nginx : force all pages to be SSL