Go Walk教程 - 流程控制( switch)

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

posted @ 2016-10-22 06:28  gobuild  阅读(2569)  评论(1编辑  收藏  举报