最新消息:20210816 当前crifan.com域名已被污染,为防止失联,请关注(页面右下角的)公众号

【已解决】go语言中的函数编译出错:syntax error: unexpected semicolon or newline before {

GO crifan 14172浏览 0评论

【问题】

折腾:

【已解决】go语言中的全局变量

期间,写了个函数:

//do some init for crifanLib
func init()
{
    fmt.Println("init something");
    curCookies = nil
}

结果编译出错:

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

即:

syntax error: unexpected semicolon or newline before {

【解决过程】

1.然后,看起来,好像是函数后面的大括号的问题,所以改为:

//do some init for crifanLib
func init(){
    fmt.Println("init something");
    curCookies = nil
}

结果就可以了。

 

【总结】

go语言中,函数定义,大括号,只能在行末:

func xxx() {
    //......
}

不能另起一行:

func xxx()
{
    //......
}

否则就会报错:

syntax error: unexpected semicolon or newline before {

的。

转载请注明:在路上 » 【已解决】go语言中的函数编译出错:syntax error: unexpected semicolon or newline before {

发表我的评论
取消评论

表情

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址

网友最新评论 (1)

  1. 不是都会使用IDE的么?会自动格式化掉
    天下10年前 (2014-08-14)回复
87 queries in 0.171 seconds, using 22.15MB memory