Go的 switch 非常靈活,表達式不必是常量或整數,執行的過程從上至下,直到找到匹配項,不要break; switch 後面的表達式甚至不是必需的 利子: RadioButton為單選按鈕,可以分組, radiobuttongroup和radiobuttongroupbox ...
Go的 switch 非常靈活,表達式不必是常量或整數,執行的過程從上至下,直到找到匹配項,不要break;
var score =98 var result string switch score/10 { case 9,10:result="優秀" case 8:result="良好" case 6,7:result="及格" default:result="不及格" }
switch 後面的表達式甚至不是必需的
var score =-1 var result string switch { case score<0:result="負數" case score=0:result="零" case score>0:result="正數" }
利子:
package main import ( "log" ) import ( "github.com/lxn/walk" . "github.com/lxn/walk/declarative" ) var le *walk.LineEdit var sport,maths,english *walk.RadioButton func main() { if _, err := MainWindow1.Run(); err != nil { log.Fatal(err) } log.Fatal(Bind("enabledCB.Checked")) } var MainWindow1=MainWindow{ Title: "MainWindow", MinSize: Size{300, 200}, Layout: VBox{}, Children: widget, } var widget=[]Widget{ LineEdit1, RadioButtonSport, RadioButtonMaths, RadioButtonEnglish, PushButtonOK, } var LineEdit1=LineEdit{ AssignTo: &le, } var RadioButtonSport=RadioButton{ AssignTo: &sport, Text: "體育系", } var RadioButtonMaths=RadioButton{ AssignTo: &maths, Text: "數學系", } var RadioButtonEnglish=RadioButton{ AssignTo: &english, Text: "英語系", } var PushButtonOK=PushButton{ Text: "OK", OnClicked:OK_Clicked, } func OK_Clicked(){ switch { case english.Checked(): le.SetText("英語系") case sport.Checked(): le.SetText("體育系") case maths.Checked(): le.SetText("數學系") } }
RadioButton為單選按鈕,可以分組,
radiobuttongroup和radiobuttongroupbox