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

前端 未结 10 894
终归单人心
终归单人心 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:43

    This seems to be premature microoptimization. The compiler is free to produce the same code for both cases or at least for these two

    if len(s) != 0 { ... }
    

    and

    if s != "" { ... }
    

    because the semantics is clearly equal.

提交回复
热议问题