Go's control logic for, which can be used to iterate through the data, and as a while to control the logic, but also iterative operations.
The first type, similar to C
Sum: = 0;for index:=0; Index < 10; index++ {sum + = index}
The second, for mate range, can be used to read slice and map data, similar to the foreach in some languages:
For K,v:=range map {fmt. PRINTLN ("Map ' s key:", K) fmt. PRINTLN ("Map ' s val:", v)}
The third, the control logic, replaces the while function
Sum: = 1for sum < $ {sum + = sum}
And another is the cycle of death,
I: = 0for {if i > {break}fmt. Println (i) i++}
Terminates the current loop with break
Break and Continue
The break operation is to jump out of the current loop, continue is to skip this loop and continue to the next loop.
For I: = 0; I < 10; i++ {If i > 5 {break← terminates this loop, printing only 0 to 5}println (i)}
Lizi:
Copyright: The Walk Authors. All rights reserved.//Use of this source code are governed by a bsd-style//license so can be found in the license file. Package Mainimport ("FMT" "Log" "OS" "Strings") Import ("Github.com/lxn/walk"). "Github.com/lxn/walk/declarative") func main () {mw: = &mymainwindow{model:newenvmodel ()}if _, Err: = (mainwindow{ Assignto: &MW. Mainwindow,title: "Walk ListBox Example", minsize:size{240, 320},size:size{300, 400},layout:vbox{marginszero: True},children: []widget{vsplitter{children: []widget{listbox{assignto: &mw.lb,model:mw.model, Oncurrentindexchanged:mw.lb_currentindexchanged,onitemactivated:mw.lb_itemactivated,},textedit{assignto: & Mw.te,readonly:true,},},},},}. Run ()); Err! = Nil {log. Fatal (err)}}type mymainwindow struct {*walk. Mainwindowmodel *envmodellb *walk. Listboxte *walk. Textedit}func (MW *mymainwindow) lb_currentindexchanged () {i: = Mw.lb.CurrentIndex () Item: = &mw.model.items[i] Mw.te.SetText (Item.value) fmt. PriNtln ("Currentindex:", i) fmt. Println ("Currentenvvarname:", Item.name)}func (MW *mymainwindow) lb_itemactivated () {Value: = mw.model.items[ Mw.lb.CurrentIndex ()].valuewalk. MsgBox (MW, "value", Value, walk. msgboxiconinformation)}type envitem struct {name stringvalue string}type envmodel struct {walk. Listmodelbaseitems []envitem}func Newenvmodel () *envmodel {env: = OS. Environ () M: = &envmodel{items:make ([]envitem, Len (env))}for I, E: = Range env {j: = strings. Index (E, "=") if J = = 0 {continue}name: = E[0:j]value: = Strings. Replace (e[j+1:], ";", "\ r \ n",-1) m.items[i] = Envitem{name, Value}}return m}func (M *envmodel) ItemCount () int {return len (M.items)} Func (M *envmodel) Value (index int) interface{} {return m.items[index].name}
Walk Lizi
——
Go Walk Tutorial-Process Control (for)