您好,登錄后才能下訂單哦!
golang判斷字符串是否為空的方法:
var s string if s=="" { }
或:
if len(s)==0{}
推薦使用第一種方法判斷字符串是否為空,就字符串是否為空這個(gè)語(yǔ)義而言,第一種語(yǔ)義更直接了當(dāng),應(yīng)該是更推薦的。
> 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判斷字符串是否為空的方法的詳細(xì)內(nèi)容,更多請(qǐng)關(guān)注億速云其它相關(guān)文章!
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。