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

【已解决】go语言编译出错:syntax error: unexpected :, expecting := or = or comma

GO crifan 25757浏览 0评论

【问题】

折腾:

【已解决】go语言中实现输出内容到log文件

期间,用如下代码:

    var logFilename string =  filenameOnly + ".log";
    //logfile,err: = os.OpenFile("/Users/cybercare/tmp/test.log",os.O_RDWR|os.O_CREATE,0666)
    logFile, err: = os.OpenFile(logFilename, os.O_RDWR|os.O_CREATE, 0777)
    if err!=nil{
        fmt.Printf("open file error=%s\r\n",err.Error())
        os.Exit(-1)
    }

    defer logFile.Close()
    logger:=log.New(logFile,"\r\n", log.Ldate | log.Ltime | log.Llongfile)
    logger.Println("hello")
    logger.Println("oh....")
    logger.Fatal("test")
    logger.Fatal("test2")

结果出错:

D:\tmp\tmp_dev_root\go\src\github.com\user\EmulateLoginBaidu>go run EmulateLoginBaidu.go
# command-line-arguments
.\EmulateLoginBaidu.go:55: syntax error: unexpected :, expecting := or = or comma

如图:

EmulateLoginBaidu.go syntax error unexpected expecting or or comma

即:

syntax error: unexpected :, expecting := or = or comma

【解决过程】

1.去把import中的os加上,改为:

package main

import (
    "fmt"
    //"log"
    "os"
    //"io/ioutil"
    //"net/http"
    "runtime"
    "path"
    "strings"
)

// GetCurFilename
// Get current file name, without suffix
func GetCurFilename() string {
    // var currentDir string
    // currentDir, err := os.Getwd()
    // fmt.Println("currentDir=%s", currentDir)
    // if err != nil {
        // fmt.Println("get current directory error=%s\n", err)
    // }

    _, fulleFilename, _, _ := runtime.Caller(0)
    //fmt.Println(fulleFilename)
    var filenameWithSuffix string
    filenameWithSuffix = path.Base(fulleFilename)
    //fmt.Println("filenameWithSuffix=", filenameWithSuffix)
    var fileSuffix string
    fileSuffix = path.Ext(filenameWithSuffix)
    //fmt.Println("fileSuffix=", fileSuffix)
    
    //filenameWithSuffix = "EmulateLoginBaidu"
    //fileSuffix = ".go"
    //filenameWithSuffix = "EmulateLoginBaidu.go.go.go"
    //fileSuffix = ".go"
    //filenameWithSuffix = "EmulateLoginBaidu.go.txt"
    //fileSuffix = ".go"
    var filenameOnly string
    filenameOnly = strings.TrimSuffix(filenameWithSuffix, fileSuffix)
    //fmt.Println("filenameOnly=", filenameOnly)
    
	return filenameOnly
}

func main() {
    fmt.Printf("this is EmulateLoginBaidu.go\n")
    
    var filenameOnly string
    filenameOnly = GetCurFilename()
    fmt.Println("filenameOnly=", filenameOnly)
    
    var logFilename string =  filenameOnly + ".log";
    //logfile,err: = os.OpenFile("/Users/cybercare/tmp/test.log",os.O_RDWR|os.O_CREATE,0666)
    logFile, err: = os.OpenFile(logFilename, os.O_RDWR | os.O_CREATE, 0777)
    if err!=nil{
        fmt.Printf("open file error=%s\r\n",err.Error())
        os.Exit(-1)
    }

    defer logFile.Close()
    logger:=log.New(logFile,"\r\n", log.Ldate | log.Ltime | log.Llongfile)
    logger.Println("hello")
    logger.Println("oh....")
    logger.Fatal("test")
    logger.Fatal("test2")
    
    //var baiduMainUrl string
    //baiduMainUrl = "http://www.baidu.com/";
    //baiduMainUrl := "http://www.baidu.com/";
    // var baiduMainUrl string = "http://www.baidu.com/";
    // fmt.Printf("baiduMainUrl=%s\n", baiduMainUrl)
    // resp, err := http.Get(baiduMainUrl)
    // if err != nil {
        // fmt.Printf("http get response errror=%s\n", err)
    // }
    // defer resp.Body.Close()
    // body, err := ioutil.ReadAll(resp.Body)
    // fmt.Printf("body=%s\n", body)
}

结果:

错误依旧。

2.再改为:

    //logFile, err: = os.OpenFile(logFilename, os.O_RDWR | os.O_CREATE, 0777)
    logFile, err: = os.OpenFile(logFilename, os.O_RDWR | os.O_CREATE, 0777);

结果错误依旧。

3.参考:

In an import clause: "syntax error: unexpected :, expecting := or = or comma" — wot?

结果也是没有太大帮助。

4.此处,经过自己认真看代码,发现错误了:

自己不小心,把

:=

写成了

: =

即,冒号和等号之间,多了个空格。

所以出现语法错误,改为:

    //logfile,err: = os.OpenFile("/Users/cybercare/tmp/test.log",os.O_RDWR|os.O_CREATE,0666)
    //logFile, err: = os.OpenFile(logFilename, os.O_RDWR | os.O_CREATE, 0777)
    //logFile, err: = os.OpenFile(logFilename, os.O_RDWR | os.O_CREATE, 0777);
    logFile, err := os.OpenFile(logFilename, os.O_RDWR | os.O_CREATE, 0777)

即可。

 

【总结】

此处,go代码:

logFile, err: = os.OpenFile(logFilename, os.O_RDWR | os.O_CREATE, 0777)

出现:

syntax error: unexpected :, expecting := or = or comma

的错误,原因是:

自己的笔误,在给原先参考别人的代码,没有空格,而自己手动去加空格的时候,不小心把:

冒号等于号

弄成了:

冒号空格等于号

所以出错了。

改为正确的:

:=

即可。

转载请注明:在路上 » 【已解决】go语言编译出错:syntax error: unexpected :, expecting := or = or comma

发表我的评论
取消评论

表情

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

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址
84 queries in 0.162 seconds, using 22.17MB memory