【问题】
折腾:
期间,去运行代码:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 | package main import ( "fmt" "log" "os" //"io/ioutil" //"net/http" "runtime" "path" "strings" ) // GetCurFilename // Get current file name, without suffix func GetCurFilename() string { _, 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) var filenameOnly string filenameOnly = strings.TrimSuffix(filenameWithSuffix, fileSuffix) //fmt.Println("filenameOnly=", filenameOnly) return filenameOnly } //get url response html func GetUrlRespHtml(url string) string{ var respHtml string = "" ; resp, err := http.Get(url) 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) respHtml = body return respHtml } func main() { fmt.Printf( "this is EmulateLoginBaidu.go\n" ) var filenameOnly string filenameOnly = GetCurFilename() fmt.Println( "filenameOnly=" , filenameOnly) var logFilename string = filenameOnly + ".log" ; fmt.Println( "logFilename=" , logFilename) //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 ) 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:=log.New(logFile, "\r\n" , log.Ldate | log.Ltime | log.Lshortfile) logger.Println( "normal log 1" ) logger.Println( "normal log 2" ) //【已解决】go代码出错退出:exit status 1 //logger.Panic("panic 1") //logger.Fatal("fatal 1") var baiduMainUrl string //baiduMainUrl := "http://www.baidu.com/"; //var baiduMainUrl string = "http://www.baidu.com/"; fmt.Printf( "baiduMainUrl=%s" , baiduMainUrl) respHtml := GetUrlRespHtml(baiduMainUrl) logger.Printf( "respHtml=%s" , respHtml) } |
结果出错:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 | D:\tmp\tmp_dev_root\go\src\github.com\user\EmulateLoginBaidu>go run EmulateLoginBaidu.go # command-line-arguments .\EmulateLoginBaidu.go:73: illegal UTF-8 sequence a1 be .\EmulateLoginBaidu.go:73: illegal UTF-8 sequence d2 d1 .\EmulateLoginBaidu.go:73: illegal UTF-8 sequence bd e2 .\EmulateLoginBaidu.go:73: illegal UTF-8 sequence be f6 .\EmulateLoginBaidu.go:73: illegal UTF-8 sequence a1 bf .\EmulateLoginBaidu.go:73: illegal UTF-8 sequence b4 fa .\EmulateLoginBaidu.go:73: illegal UTF-8 sequence c2 eb .\EmulateLoginBaidu.go:73: illegal UTF-8 sequence b3 f6 .\EmulateLoginBaidu.go:73: illegal UTF-8 sequence b4 ed .\EmulateLoginBaidu.go:73: illegal UTF-8 sequence .\EmulateLoginBaidu.go:73: too many errors D:\tmp\tmp_dev_root\go\src\github.com\user\EmulateLoginBaidu> |
如图:
即:
illegal UTF-8 sequence |
【解决过程】
1.很明显,是字符编码问题。
2.去看了看当前的文件,果然是GBK的:
所以,去改为UTF-8:
然后再去重新编译代码,果然就可以了。
至少没有这个utf-8的错误提示了。
【总结】
go代码的文件编码中,如果包含非英文字符,比如此处的中文,如果go文件本身是非UTF-8编码,即使是放在注释里面,也是不行的。
必须要确保go文件本身是UTF-8编码的才可以的。
注:
Notepad++可以实现文件编码转换,不熟悉的可以参考:
转载请注明:在路上 » 【问题】go代码运行出错:# command-line-arguments .\EmulateLoginBaidu.go:86: illegal UTF-8 sequence