golang判斷字串是否為空的方法:
var s string if s=="" { }
或:
if len(s)==0{}
推薦使用第一種方法判斷字串是否為空,就字串是否為空這個語意而言,第一種語意更直接了當,應該是更推薦的。
> The one that makes the code clear.
If I'm about to look at element x I typically write
len(s) > x, even for x = = 0, but if I care about
"is it this specific string" I tend to write s == "".
>
>It's reasonable to assume that a mature compiler will compile
#len(s) == 0 and s == "" into the same, efficient code.
Right now 6g etc do compile s == "" into a function call
while len(s) == 0 is not, but that's been on my to-do list to fix.
#>Make the code clear.
>
>Russ
更多golang知識請關注golang教程欄位。
以上是golang判斷字串是否為空的方法的詳細內容。更多資訊請關注PHP中文網其他相關文章!