第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

Switch case 語句落入默認值

Switch case 語句落入默認值

Go
ibeautiful 2021-11-22 15:44:14
我是新手,無法弄清楚為什么最后一個 case 子句(連接和測試)會變成默認值。但是帶有換行符的那些(退出\r\n 和連接\r\n)不會沒有 fallthrough 語句。我試過標記開關并調用 break [lbl] 但默認塊仍然被執(zhí)行package mainimport ("fmt""strings""bufio""os")func main() {var cmd stringbio := bufio.NewReader(os.Stdin)fmt.Println("Hello")proceed := truefor 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)}
查看完整描述

2 回答

?
紫衣仙女

TA貢獻1839條經(jīng)驗 獲得超15個贊

Go 編程語言規(guī)范

開關語句

“Switch”語句提供多路執(zhí)行。表達式或類型說明符與內(nèi)部的“case”進行比較

表情開關

在表達式 switch 中,對 switch 表達式求值,而不必為常量的 case 表達式從左到右和從上到下求值;第一個等于 switch 表達式的語句觸發(fā)相關 case 語句的執(zhí)行;其他情況被跳過。如果沒有 case 匹配并且存在“默認” case,則執(zhí)行其語句。最多可以有一個默認情況,它可能出現(xiàn)在“switch”語句中的任何地方。缺少 switch 表達式等效于布爾值 true。

ExprSwitchStmt = "switch" [ SimpleStmt ";" ] [ Expression ] "{" { ExprCaseClause } "}" .

ExprCaseClause = ExprSwitchCase ":" StatementList .

ExprSwitchCase = "case" ExpressionList | "default" .

最后一個switch case子句 ( "connect"and "test") 不屬于default case子句。一break在一份聲明中switch聲明case條款打破了出來switch發(fā)言; 它不會脫離周圍的 for子句。


您尚未向我們提供可重現(xiàn)的示例:如何創(chuàng)建最小、完整和可驗證的示例。. 例如,您沒有向我們展示您的輸入和輸出。


這是一個示例,它按預期工作。default執(zhí)行該條款是有原因的。


>> test 127.0.0.1 8080

dont print anything else, dont fall through to default. There should be no reason why the default caluse is executed???

Unrecognised command: 127.0.0.1

Unrecognised command: 8080


>> 

的價值cmds,fmt.Printf("%q\n", cmds)是["test" "127.0.0.1" "8080\r\n"]。


您的程序邏輯存在嚴重缺陷。


查看完整回答
反對 回復 2021-11-22
?
UYOU

TA貢獻1878條經(jīng)驗 獲得超4個贊

嘗試strings.Trim()像這樣包裝 switch 語句的主題:


switch strings.TrimSpace(cmds[i]) {

   // your cases

}

但總的來說,在這種情況下,內(nèi)部 for 循環(huán)看起來是錯誤的構造?;谶@段代碼,您可能想要刪除它,并且只將cmds數(shù)組的第一個元素作為switch 語句的主題


查看完整回答
反對 回復 2021-11-22
  • 2 回答
  • 0 關注
  • 308 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網(wǎng)微信公眾號