Created
June 24, 2019 11:02
-
-
Save victorsteven/7ada2f30faa91aac92ceeabf73f1bc46 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| package main | |
| import "fmt" | |
| func main() { | |
| s := []int{10, 20, 30, 40} | |
| // We can loop through this slice in two ways: | |
| // 1. using "range" | |
| for key, value := range s { | |
| fmt.Println(key, value) | |
| } | |
| //If we dont want the key, we do, replace "key" with "_": | |
| for _, value := range s { | |
| fmt.Println(value) | |
| } | |
| // 2. Using traditional forloop: | |
| for i := 0; i < len(s); i++ { | |
| fmt.Println(s[i]) //get the value at index "i" | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment