What is the best way to test for an empty string in Go?

前端 未结 10 661
终归单人心
终归单人心 2021-01-29 20:26

Which method is best (more idomatic) for testing non-empty strings (in Go)?

if len(mystring) > 0 { }

Or:

if mystring != \"\"         


        
10条回答
  •  小鲜肉
    小鲜肉 (楼主)
    2021-01-29 20:38

    As of now, the Go compiler generates identical code in both cases, so it is a matter of taste. GCCGo does generate different code, but barely anyone uses it so I wouldn't worry about that.

    https://godbolt.org/z/fib1x1

提交回复
热议问题