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

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

    As per official guidelines and from performance point of view they appear equivalent (ANisus answer), the s != "" would be better due to a syntactical advantage. s != "" will fail at compile time if the variable is not a string, while len(s) == 0 will pass for several other data types.

    0 讨论(0)
  • 2021-01-29 20:33

    I think the best way is to compare with blank string

    BenchmarkStringCheck1 is checking with blank string

    BenchmarkStringCheck2 is checking with len zero

    I check with the empty and non-empty string checking. You can see that checking with a blank string is faster.

    BenchmarkStringCheck1-4     2000000000           0.29 ns/op        0 B/op          0 allocs/op
    BenchmarkStringCheck1-4     2000000000           0.30 ns/op        0 B/op          0 allocs/op
    
    
    BenchmarkStringCheck2-4     2000000000           0.30 ns/op        0 B/op          0 allocs/op
    BenchmarkStringCheck2-4     2000000000           0.31 ns/op        0 B/op          0 allocs/op
    

    Code

    func BenchmarkStringCheck1(b *testing.B) {
        s := "Hello"
        b.ResetTimer()
        for n := 0; n < b.N; n++ {
            if s == "" {
    
            }
        }
    }
    
    func BenchmarkStringCheck2(b *testing.B) {
        s := "Hello"
        b.ResetTimer()
        for n := 0; n < b.N; n++ {
            if len(s) == 0 {
    
            }
        }
    }
    
    0 讨论(0)
  • 2021-01-29 20:35

    It would be cleaner and less error-prone to use a function like the one below:

    func empty(s string) bool {
        return len(strings.TrimSpace(s)) == 0
    }
    
    0 讨论(0)
  • 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

    0 讨论(0)
  • 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.

    0 讨论(0)
  • 2021-01-29 20:44

    This would be more performant than trimming the whole string, since you only need to check for at least a single non-space character existing

    // Strempty checks whether string contains only whitespace or not
    func Strempty(s string) bool {
        if len(s) == 0 {
            return true
        }
    
        r := []rune(s)
        l := len(r)
    
        for l > 0 {
            l--
            if !unicode.IsSpace(r[l]) {
                return false
            }
        }
    
        return true
    }
    
    0 讨论(0)
提交回复
热议问题