Skip to content

Instantly share code, notes, and snippets.

@sugilog
Last active August 29, 2015 14:12
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save sugilog/2bedda6ab5863bed0b0b to your computer and use it in GitHub Desktop.
Save sugilog/2bedda6ab5863bed0b0b to your computer and use it in GitHub Desktop.
for-rangeループ
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