【问题】
折腾:
期间,写了个函数:
1 2 3 4 5 6 | //do some init for crifanLib func init() { fmt.Println( "init something" ); curCookies = nil } |
结果编译出错:
1 2 3 4 5 | E:\Dev_Root\go\src\EmulateLoginBaidu>go run EmulateLoginBaidu.go # command-line-arguments .\EmulateLoginBaidu.go:36: syntax error: unexpected semicolon or newline before { .\EmulateLoginBaidu.go:38: non-declaration statement outside function body .\EmulateLoginBaidu.go:39: syntax error: unexpected } |
如图:
即:
syntax error: unexpected semicolon or newline before { |
【解决过程】
1.然后,看起来,好像是函数后面的大括号的问题,所以改为:
1 2 3 4 5 | //do some init for crifanLib func init(){ fmt.Println( "init something" ); curCookies = nil } |
结果就可以了。
【总结】
go语言中,函数定义,大括号,只能在行末:
1 2 3 | func xxx() { //...... } |
不能另起一行:
1 2 3 4 | func xxx() { //...... } |
否则就会报错:
syntax error: unexpected semicolon or newline before { |
的。
转载请注明:在路上 » 【已解决】go语言中的函数编译出错:syntax error: unexpected semicolon or newline before {