2 回答
TA貢獻1906條經驗 獲得超10個贊
以防萬一這是一個 eol(行尾)問題,請嘗試:
fmt.Scanf("%s\r\n",?&i)這在“我如何fmt.Scanf在 Go 中使用”中提到:
這是因為不同的行尾。
Windows 使用回車和換行('?\r\n')作為行尾。
Unix 使用換行符('?\n')
TA貢獻1111條經驗 獲得超0個贊
我無法重現您的錯誤。
不要忽略錯誤。例如,
package main
import "fmt"
func main() {
var i string
n, err := fmt.Scanf("%s\n", &i)
if err != nil || n != 1 {
fmt.Println(n, err)
}
fmt.Println(i)
switch i {
case "a":
fmt.Println("good")
case "b":
fmt.Println("not good")
default:
fmt.Println("bad")
}
}
輸出:
C:\>go version
go version go1.3.3 windows/amd64
C:\gopath\src\so>go run test.go
a
a
good
C:\gopath\src\so>go run test.go
b
b
not good
C:\gopath\src\so>go run test.go
t
t
bad
- 2 回答
- 0 關注
- 232 瀏覽
添加回答
舉報
