Last active
August 29, 2015 14:12
-
-
Save sugilog/2bedda6ab5863bed0b0b to your computer and use it in GitHub Desktop.
for-rangeループ
This file contains 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() { | |
sl := []int{ 10, 20, 30, 40, 50, 60, 70, 80, 90 } | |
fmt.Println( "rangeWith2Var" ) | |
rangeWith2Var( sl ) | |
fmt.Println( "rangeWith1VarForIndex" ) | |
rangeWith1VarForIndex( sl ) | |
fmt.Println( "rangeWith1VarForValue" ) | |
rangeWith1VarForValue( sl ) | |
fmt.Println( "rangeWithoutVar" ) | |
rangeWithoutVar( sl ) | |
} | |
func rangeWith2Var( slice []int ) { | |
for index, value := range slice { | |
fmt.Println( index, value ) | |
} | |
} | |
func rangeWith1VarForIndex( slice []int ) { | |
for index := range slice { | |
fmt.Println( index ) | |
} | |
} | |
func rangeWith1VarForValue( slice []int ) { | |
for _, value := range slice { | |
// fmt.Println( _ ) | |
fmt.Println( value ) | |
} | |
} | |
func rangeWithoutVar( slice []int ) { | |
for range slice { | |
fmt.Print( "*" ) | |
} | |
fmt.Println() | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment