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.1k Golang : Calculate US Dollar Index (DXY)
+8.9k Golang : Populate or initialize struct with values example
+4.6k MariaDB/MySQL : How to get version information
+6.3k Golang : How to search a list of records or data structures
+9.9k Golang : Convert octal value to string to deal with leading zero problem
+13.2k CodeIgniter : "Fatal error: Cannot use object of type stdClass as array" message
+7k Golang : A simple forex opportunities scanner
+8.7k Golang : Take screen shot of browser with JQuery example
+11.6k How to tell if a binary(executable) file or web application is built with Golang?
+15.6k Golang : Get checkbox or extract multipart form data value example
+9.1k Golang : Write multiple lines or divide string into multiple lines
+4.8k Python : Find out the variable type and determine the type with simple test