Which method is best (more idomatic) for testing non-empty strings (in Go)?
if len(mystring) > 0 { }
Or:
if mystring != "" { }
Or something else?
gois-emptystring
Which method is best (more idomatic) for testing non-empty strings (in Go)?
if len(mystring) > 0 { }
Or:
if mystring != "" { }
Or something else?
Best Answer
Both styles are used within the Go's standard libraries.
can be found in the
strconv
package: http://golang.org/src/pkg/strconv/atoi.gocan be found in the
encoding/json
package: http://golang.org/src/pkg/encoding/json/encode.goBoth are idiomatic and are clear enough. It is more a matter of personal taste and about clarity.
Russ Cox writes in a golang-nuts thread:
As pointed out in Timmmm's answer, the Go compiler does generate identical code in both cases.