Guest User

Untitled

a guest
Jan 16th, 2018
79
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.97 KB | None | 0 0
  1. package main
  2.  
  3. import "fmt"
  4.  
  5. var pow = []int{1, 2, 4, 8, 16, 32, 64, 128}
  6.  
  7. var persons = []Person {
  8. {"Miguel", "Fermin"},
  9. {"Ana", "Concepcion"},
  10. {"Noah", "Fermin"},
  11. {"Milady", "Perez"},
  12. {"Hailey", "Diaz"},
  13. }
  14.  
  15. func main() {
  16. // The range form of the for loop iterates over a slice or map.
  17. // When ranging over a slice, two values are returned for each iteration.
  18. // The first is the index, and the second is a copy of the element at that index.
  19. for i, v := range pow {
  20. fmt.Printf("2**%d = %d\n", i, v)
  21. }
  22.  
  23. // You can skip the index or value by assigning to _.
  24. fmt.Println()
  25. for _, person := range persons {
  26. fmt.Printf("person: %v\n", person.name())
  27. }
  28.  
  29. // If you only want the index, drop the ", value" entirely.
  30. fmt.Println()
  31. for index, _ := range persons {
  32. fmt.Printf("index: %v\n", index)
  33. }
  34. fmt.Println()
  35. }
  36.  
  37. // Supporting Types
  38.  
  39. type Person struct {
  40. firstName, lastName string
  41. }
  42.  
  43. func (p Person) name() string {
  44. return fmt.Sprintf("%v %v", p.firstName, p.lastName)
  45. }
Add Comment
Please, Sign In to add comment