<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet type="text/xsl" href="default.xsl"?>
<fr:tree
toc="true"
numbered="true"
show-heading="true"
show-metadata="true"
expanded="true"
root="false"
xmlns:fr="http://www.jonmsterling.com/jms-005P.xml"><fr:frontmatter><fr:anchor>401</fr:anchor><fr:addr
type="user">go-00si</fr:addr><fr:route>go-00si.xml</fr:route><fr:title
text="Golang › Control Flow › for"><fr:link
type="local"
href="go-00sg.xml"
addr="go-00sg"
title="Golang › Control Flow"><fr:link
type="local"
href="go-00rs.xml"
addr="go-00rs"
title="Golang">Golang</fr:link> › Control Flow</fr:link> › for</fr:title><fr:authors /></fr:frontmatter><fr:mainmatter><fr:p>Loops in go support complete (C-style), condition only, for-range styles.</fr:p><fr:pre><![CDATA[// C-style
for i := 0; i < 10; i++ {
    fmt.Println(i)
}

// condition only (while)
i := 1
for i < 10 {
    fmt.Println(i)
    i++
}

// for-range
nums := []int{1, 2, 3}
for i, v := range nums {
    fmt.Println(i, v)
}]]></fr:pre><fr:p>If you don't need the key/index in,  the underscore <fr:code>_</fr:code> can be used, telling go to ignore the value.</fr:p><fr:pre><![CDATA[evenVals := []int{2, 4, 6, 8, 10, 12}
for _, v := range evenVals {
    fmt.Printf("%d ", v)
}]]></fr:pre><fr:p>If you just need the key/index, you can leave off the second variable:</fr:p><fr:pre><![CDATA[uniqueNames := map[string]bool{"Fred": true, "Raul": true, "Wilma": true}
for k := range uniqueNames {
    fmt.Printf("%s ", k)
}]]></fr:pre></fr:mainmatter><fr:backmatter><fr:tree
toc="false"
numbered="false"
show-heading="true"
show-metadata="false"
expanded="true"
root="false"
xmlns:fr="http://www.jonmsterling.com/jms-005P.xml"><fr:frontmatter><fr:title
text="Context">Context</fr:title><fr:authors /></fr:frontmatter><fr:mainmatter><fr:tree
toc="true"
numbered="false"
show-heading="true"
show-metadata="true"
expanded="false"
root="false"
xmlns:fr="http://www.jonmsterling.com/jms-005P.xml"><fr:frontmatter><fr:anchor>402</fr:anchor><fr:addr
type="user">go-00sg</fr:addr><fr:route>go-00sg.xml</fr:route><fr:title
text="Golang › Control Flow"><fr:link
type="local"
href="go-00rs.xml"
addr="go-00rs"
title="Golang">Golang</fr:link> › Control Flow</fr:title><fr:authors /></fr:frontmatter><fr:mainmatter><fr:tree
toc="true"
numbered="true"
show-heading="true"
show-metadata="false"
expanded="true"
root="false"
xmlns:fr="http://www.jonmsterling.com/jms-005P.xml"><fr:frontmatter><fr:anchor>280</fr:anchor><fr:addr
type="user">go-00sh</fr:addr><fr:route>go-00sh.xml</fr:route><fr:title
text="If">If</fr:title><fr:authors /></fr:frontmatter><fr:mainmatter><fr:p>Go's `if` statements do not require parentheses. You can declare variables scoped to the condition:</fr:p><fr:pre><![CDATA[if n := rand.Intn(10); n > 5 {
    fmt.Println("Big:", n)
} else {
    fmt.Println("Small:", n)
}]]></fr:pre></fr:mainmatter><fr:backmatter /></fr:tree><fr:tree
toc="true"
numbered="true"
show-heading="true"
show-metadata="false"
expanded="true"
root="false"
xmlns:fr="http://www.jonmsterling.com/jms-005P.xml"><fr:frontmatter><fr:anchor>281</fr:anchor><fr:addr
type="user">go-00si</fr:addr><fr:route>go-00si.xml</fr:route><fr:title
text="For">For</fr:title><fr:authors /></fr:frontmatter><fr:mainmatter><fr:p>Loops in go support complete (C-style), condition only, for-range styles.</fr:p><fr:pre><![CDATA[// C-style
for i := 0; i < 10; i++ {
    fmt.Println(i)
}

// condition only (while)
i := 1
for i < 10 {
    fmt.Println(i)
    i++
}

// for-range
nums := []int{1, 2, 3}
for i, v := range nums {
    fmt.Println(i, v)
}]]></fr:pre><fr:p>If you don't need the key/index in,  the underscore <fr:code>_</fr:code> can be used, telling go to ignore the value.</fr:p><fr:pre><![CDATA[evenVals := []int{2, 4, 6, 8, 10, 12}
for _, v := range evenVals {
    fmt.Printf("%d ", v)
}]]></fr:pre><fr:p>If you just need the key/index, you can leave off the second variable:</fr:p><fr:pre><![CDATA[uniqueNames := map[string]bool{"Fred": true, "Raul": true, "Wilma": true}
for k := range uniqueNames {
    fmt.Printf("%s ", k)
}]]></fr:pre></fr:mainmatter><fr:backmatter /></fr:tree><fr:tree
toc="true"
numbered="true"
show-heading="true"
show-metadata="false"
expanded="true"
root="false"
xmlns:fr="http://www.jonmsterling.com/jms-005P.xml"><fr:frontmatter><fr:anchor>282</fr:anchor><fr:addr
type="user">go-00sj</fr:addr><fr:route>go-00sj.xml</fr:route><fr:title
text="Switch">Switch</fr:title><fr:authors /></fr:frontmatter><fr:mainmatter><fr:pre><![CDATA[words := []string{"a", "cow", "smile", "gopher",
    "octopus", "anthropologist"}
for _, word := range words {
    switch size := len(word); size {
    case 1, 2, 3, 4:
        fmt.Println(word, "is a short word!")
    case 5:
        wordLen := len(word)
        fmt.Println(word, "is exactly the right length:", wordLen)
    case 6, 7, 8, 9:
    default:
        fmt.Println(word, "is a long word!")
    }
}]]></fr:pre></fr:mainmatter><fr:backmatter /></fr:tree></fr:mainmatter><fr:backmatter /></fr:tree></fr:mainmatter><fr:backmatter /></fr:tree></fr:backmatter></fr:tree>