👊
Go tutorial for Pythoner
  • Introduction
  • Install and Run Go
  • The structure of Go
    • First time you meet him
    • Functions
    • Return multiple results
    • Logic Control
    • Break loop and enumerate
    • List (Array or Slice)
    • Dict (Map)
    • Struct
    • Class (Methods)
    • Packages
  • Features of Go
  • Go modules
  • Web Server
    • Serving static files
    • A simple webserver
  • Go for Android
  • Where to go next
Powered by GitBook
On this page
  • Break a loop
  • Enumerate

Was this helpful?

  1. The structure of Go

Break loop and enumerate

Break a loop

package main

import "fmt"

func main() {
    i := 0
    for true { //go to infinty
        fmt.Println(i)
        if i == 10 {
            break
        }
        i += 1
    }
}

Enumerate

package main

import (
    "fmt"
    "strings"
)

func traverse_an_array(sentences []string) {
    for index, sentence := range sentences {
        fmt.Printf("\n%d. %s.\n", index, strings.Trim(sentence, ". "))
    }
    // if you don't want to use index, use:
    // for _, sentence := range sentences
}

func main() {
    poem := `Welcome to part 12 of the Go programming tutorial series, where we're going to cover looping/iterating in Go. Leading up to this, we've been working on a project that aggregates news. We're at the point where we have a list of sitemaps that we want to iterate over in order to visit. In the Go language, there is only the "for" loop, there is no "while" statement, but we can still mimic what we might traditionally put in a while loop.`
    sentences := strings.Split(poem, ". ")
    traverse_an_array(sentences)
}
PreviousLogic ControlNextList (Array or Slice)

Last updated 4 years ago

Was this helpful?