我是新手,无法弄清楚为什么最后一个 case 子句(连接和测试)会变成默认值。但是带有换行符的那些(退出\r\n 和连接\r\n)不会
没有 fallthrough 语句。
我试过标记开关并调用 break [lbl] 但默认块仍然被执行
package main
import (
"fmt"
"strings"
"bufio"
"os"
)
func main() {
var cmd string
bio := bufio.NewReader(os.Stdin)
fmt.Println("Hello")
proceed := true
for proceed {
fmt.Print(">> ")
cmd, _ = bio.ReadString('\n')
cmds := strings.Split(cmd, " ")
for i := range cmds{
switch cmds[i]{
case "exit\r\n" :
proceed = false
case "connect\r\n":
fmt.Println("The connect command requires more input")
case "connect":
if i + 2 >= len(cmds) {
fmt.Println("Connect command usage: connect host port")
} else {
i++
constring := cmds[i]
i++
port := cmds[i]
con(constring, port)
}
fmt.Println("dont print anything else, dont fall through to default. There should be no reason why the default caluse is executed???")
case "test":
fmt.Println("dont print anything else, dont fall through to default. There should be no reason why the default caluse is executed???")
default:
fmt.Println("Unrecognised command: " + cmds[i])
}
}
}
}
func con (conStr, port string){
panic (conStr)
}
紫衣仙女
UYOU
相关分类