Go Walk Tutorial-Process Control (for)

Source: Internet
Author: User
Tags terminates

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: &AMP;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)

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.