Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package main
- import "fmt"
- var pow = []int{1, 2, 4, 8, 16, 32, 64, 128}
- var persons = []Person {
- {"Miguel", "Fermin"},
- {"Ana", "Concepcion"},
- {"Noah", "Fermin"},
- {"Milady", "Perez"},
- {"Hailey", "Diaz"},
- }
- func main() {
- // The range form of the for loop iterates over a slice or map.
- // When ranging over a slice, two values are returned for each iteration.
- // The first is the index, and the second is a copy of the element at that index.
- for i, v := range pow {
- fmt.Printf("2**%d = %d\n", i, v)
- }
- // You can skip the index or value by assigning to _.
- fmt.Println()
- for _, person := range persons {
- fmt.Printf("person: %v\n", person.name())
- }
- // If you only want the index, drop the ", value" entirely.
- fmt.Println()
- for index, _ := range persons {
- fmt.Printf("index: %v\n", index)
- }
- fmt.Println()
- }
- // Supporting Types
- type Person struct {
- firstName, lastName string
- }
- func (p Person) name() string {
- return fmt.Sprintf("%v %v", p.firstName, p.lastName)
- }
Add Comment
Please, Sign In to add comment