1 引言
用windows自帶的text文本在最前面會帶入看不到的BOM,導致異常
2 代碼
package main import ( "strings" "fmt" ) func main(){ line := "// mysql數據庫鏈接配置" if strings.EqualFold(line[:2], "//"){ fmt.Println("this is comment") }else{ fmt.Println("this is not comment") } }
運行結果如下:
# command-line-arguments test\test\testFilterRemark.go:17:11: invalid BOM in the middle of the file Compilation finished with exit code 2
如果改為:(在文本上是看不出差異性的)
func main(){ line := "// mysql數據庫鏈接配置" if strings.EqualFold(line[:2], "//"){ fmt.Println("this is comment") }else{ fmt.Println("this is not comment") } }
運行結果如下:
this is comment Process finished with exit code 0
3.擴展資料
1.不要使用Windows自帶記事本寫代碼( http://rabbitfeet.net/archives/144.html)