Skip to content

Instantly share code, notes, and snippets.

@sugilog

sugilog/sample.go

Last active Aug 29, 2015
Embed
What would you like to do?
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
You can’t perform that action at this time.