对于测试非空字符串(在Go中),哪种方法是最好的(最常用的)?

if len(mystring) > 0 { }

Or:

if mystring != "" { }

还是别的什么?


当前回答

检查长度是一个很好的答案,但您也可以解释一个“空”字符串,它也只是空白。严格来说不是空的,但如果你愿意检查:

package main

import (
  "fmt"
  "strings"
)

func main() {
  stringOne := "merpflakes"
  stringTwo := "   "
  stringThree := ""

  if len(strings.TrimSpace(stringOne)) == 0 {
    fmt.Println("String is empty!")
  }

  if len(strings.TrimSpace(stringTwo)) == 0 {
    fmt.Println("String two is empty!")
  }

  if len(stringTwo) == 0 {
    fmt.Println("String two is still empty!")
  }

  if len(strings.TrimSpace(stringThree)) == 0 {
    fmt.Println("String three is empty!")
  }
}

其他回答

我认为== ""更快,更易读。

package main 

import(
    "fmt"
)
func main() {
    n := 1
    s:=""
    if len(s)==0{
        n=2
    }
    fmt.Println("%d", n)
}

当DLV调试操场。go cmp与len(s)和==""我明白了 S == ""情况

    playground.go:6         0x1008d9d20     810b40f9        MOVD 16(R28), R1       
    playground.go:6         0x1008d9d24     e28300d1        SUB $32, RSP, R2       
    playground.go:6         0x1008d9d28     5f0001eb        CMP R1, R2             
    playground.go:6         0x1008d9d2c     09070054        BLS 56(PC)             
    playground.go:6         0x1008d9d30*    fe0f16f8        MOVD.W R30, -160(RSP)  

    playground.go:6         0x1008d9d34     fd831ff8        MOVD R29, -8(RSP)      
    playground.go:6         0x1008d9d38     fd2300d1        SUB $8, RSP, R29       
    playground.go:7         0x1008d9d3c     e00340b2        ORR $1, ZR, R0         

    playground.go:7         0x1008d9d40     e01f00f9        MOVD R0, 56(RSP)       
    playground.go:8         0x1008d9d44     ff7f05a9        STP (ZR, ZR), 80(RSP)  

    playground.go:9         0x1008d9d48     01000014        JMP 1(PC)                        
    playground.go:10        0x1008d9d4c     e0037fb2        ORR $2, ZR, R0         

len (s) = = 0的情况

    playground.go:6         0x100761d20     810b40f9        MOVD 16(R28), R1       
    playground.go:6         0x100761d24     e2c300d1        SUB $48, RSP, R2       
    playground.go:6         0x100761d28     5f0001eb        CMP R1, R2             
    playground.go:6         0x100761d2c     29070054        BLS 57(PC)             
    playground.go:6         0x100761d30*    fe0f15f8        MOVD.W R30, -176(RSP)  

    playground.go:6         0x100761d34     fd831ff8        MOVD R29, -8(RSP)      
    playground.go:6         0x100761d38     fd2300d1        SUB $8, RSP, R29       
    playground.go:7         0x100761d3c     e00340b2        ORR $1, ZR, R0         

    playground.go:7         0x100761d40     e02300f9        MOVD R0, 64(RSP)       
    playground.go:8         0x100761d44     ff7f06a9        STP (ZR, ZR), 96(RSP)  
    playground.go:9         0x100761d48     ff2700f9        MOVD ZR, 72(RSP)       

    playground.go:9         0x100761d4c     01000014        JMP 1(PC)              
    playground.go:10        0x100761d50     e0037fb2        ORR $2, ZR, R0         
    playground.go:10        0x100761d54     e02300f9        MOVD R0, 64(RSP)       
    playground.go:10        0x100761d58     01000014        JMP 1(PC)      
    playground.go:6         0x104855d2c     09070054        BLS 56(PC)        

引用

这似乎是不成熟的微优化。编译器可以为这两种情况或至少为这两种情况生成相同的代码

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

and

if s != "" { ... }

因为语义显然是相等的。

使用下面这样的函数会更简洁,更不容易出错:

func empty(s string) bool {
    return len(strings.TrimSpace(s)) == 0
}

这将比修整整个字符串的性能更好,因为您只需要检查至少一个存在的非空格字符

// 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
}

这两种样式都在Go的标准库中使用。

if len(s) > 0 { ... }

可以在strconv包中找到:http://golang.org/src/pkg/strconv/atoi.go

if s != "" { ... }

可以在encoding/json包中找到:http://golang.org/src/pkg/encoding/json/encode.go

两者都是惯用的,而且都足够清楚。这更多的是个人品味和清晰度的问题。

Russ Cox在golang-nuts的帖子中写道:

使代码清晰的那个。 如果我要查看元素x,我通常会写 len(s) >x,即使x == 0,但如果我关心 "是这个特定的字符串"我倾向于写s == "" 假设一个成熟的编译器会编译是合理的 Len (s) == 0和s == ""变成了同样高效的代码。 ... 使代码清晰。

正如Timmmm的回答所指出的,在这两种情况下,Go编译器确实生成了相同的代码。