multiple-line 'expression' of if-statement not supported?

51 views
Skip to first unread message

xiangd...@gmail.com

unread,
Jun 11, 2020, 2:05:21 AM6/11/20
to golang-nuts
Hi all,

Wondering if the 'expression' part of an if-statement should be in one line only, given the following case, 'go tool compile' will report a syntax error

Enter code here...
package p

func cf1() int {
        return 0
}

func cf2() int {
        return 10
}

func f() {
        if cf2() - 1
                < cf1() {
                println("tested")
        }
}


ss.go:13:3: syntax error: unexpected <, expecting expression

Guess there might be syntax specification rules but I failed to find out. 

Thanks a lot.

Ian Lance Taylor

unread,
Jun 11, 2020, 2:17:23 AM6/11/20
to xiangd...@gmail.com, golang-nuts
It's because of automatic semicolon insertion. You need to put the
"<" at the end of the "if" line, not at the start of the next line.
See https://golang.org/ref/spec#Semicolons .

Ian

xiangd...@gmail.com

unread,
Jun 11, 2020, 2:29:51 AM6/11/20
to golang-nuts
Thanks a lot, Ian, it works now.

On Thursday, June 11, 2020 at 2:17:23 PM UTC+8, Ian Lance Taylor wrote:
Reply all
Reply to author
Forward
0 new messages