Preferred way to check for an empty string?

8,029 views
Skip to first unread message

Mark Summerfield

unread,
Jul 12, 2011, 6:54:58 AM7/12/11
to golang-nuts
Hi,

There are two obvious ways to check for an empty string:

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

I like the first one since it seems slightly more obvious.

Is there a preferred form?

Thanks!

--
Mark Summerfield, Qtrac Ltd, www.qtrac.eu
C++, Python, Qt, PyQt - training and consultancy
"Programming in Python 3" - ISBN 0321680561
http://www.qtrac.eu/py3book.html

Jan Mercl

unread,
Jul 12, 2011, 7:12:21 AM7/12/11
to golan...@googlegroups.com
On Tuesday, July 12, 2011 12:54:58 PM UTC+2, Mark wrote:

There are two obvious ways to check for an empty string:

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

I like the first one since it seems slightly more obvious.

Is there a preferred form?

It's up to your preferences. Generated code for f() and g() is the same:
 
$ cat main.go
package main

func f(s string) {
    if s == "" {
        println("f")
    }
}

func g(s string) {
    if len(s) == 0 {
        println("f")
    }
}

func main() {
    s := "abc"
    f(s)
    g(s)
}
$ 6g -S main.go 

--- prog list "f" ---
0000 (main.go:3) TEXT    f+0(SB),$16-16
0001 (main.go:4) JMP     ,3
0002 (main.go:4) JMP     ,13
0003 (main.go:4) MOVL    s+8(FP),BX
0004 (main.go:4) CMPL    BX,$0
0005 (main.go:4) JNE     ,2
0006 (main.go:5) LEAQ    go.string."f"+0(SB),SI
0007 (main.go:5) LEAQ    (SP),DI
0008 (main.go:5) MOVSQ   ,
0009 (main.go:5) MOVSQ   ,
0010 (main.go:5) CALL    ,runtime.printstring+0(SB)
0011 (main.go:5) CALL    ,runtime.printnl+0(SB)
0012 (main.go:4) JMP     ,13
0013 (main.go:7) RET     ,

--- prog list "g" ---
0014 (main.go:9) TEXT    g+0(SB),$16-16
0015 (main.go:10) JMP     ,17
0016 (main.go:10) JMP     ,27
0017 (main.go:10) MOVL    s+8(FP),BX
0018 (main.go:10) CMPL    BX,$0
0019 (main.go:10) JNE     ,16
0020 (main.go:11) LEAQ    go.string."f"+0(SB),SI
0021 (main.go:11) LEAQ    (SP),DI
0022 (main.go:11) MOVSQ   ,
0023 (main.go:11) MOVSQ   ,
0024 (main.go:11) CALL    ,runtime.printstring+0(SB)
0025 (main.go:11) CALL    ,runtime.printnl+0(SB)
0026 (main.go:10) JMP     ,27
0027 (main.go:13) RET     ,

--- prog list "main" ---
...
$

yy

unread,
Jul 12, 2011, 7:12:47 AM7/12/11
to Mark Summerfield, golang-nuts
2011/7/12 Mark Summerfield <li...@qtrac.plus.com>:

> Hi,
>
> There are two obvious ways to check for an empty string:
>
>    if s == "" { ... }
>    if len(s) == 0 { ... }
>
> I like the first one since it seems slightly more obvious.
>
> Is there a preferred form?
>
> Thanks!
>

You already asked this same question one year ago:
http://groups.google.com/group/golang-nuts/browse_thread/thread/9d3478bec3c7c68d/998bb93f806bf052

I thought we had agreed s + "badgerbadgerbadger" ==
"badgerbadgerbadger" was the preferred way.


--
- yiyus || JGL .

peterGo

unread,
Jul 12, 2011, 7:14:17 AM7/12/11
to golang-nuts
Mark,

How to check a string is empty?
http://groups.google.com/group/golang-nuts/msg/e7b09827a3a3eb18

Peter

Russ Cox

unread,
Jul 12, 2011, 12:07:39 PM7/12/11
to yy, Mark Summerfield, golang-nuts
On Tue, Jul 12, 2011 at 04:12, yy <yiyu...@gmail.com> wrote:
> s + "badgerbadgerbadger" == "badgerbadgerbadger"

Seems non-idiomatic. Shouldn't it be

s + "gophergophergopher" == "gophergophergopher"

?

Ultimately it's a matter of taste.
There's no effect on the generated code.

Russ

Reply all
Reply to author
Forward
0 new messages