Created
November 10, 2015 02:44
-
-
Save liggitt/d9b857b4a9e213a9e398 to your computer and use it in GitHub Desktop.
Go string ranging behavior
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() { | |
s := "样本" | |
fmt.Println(len(s)) | |
fmt.Println("") | |
fmt.Println("range index only:") | |
for i := range s { | |
fmt.Print(i, " ") | |
} | |
fmt.Println("") | |
fmt.Println("range index/unused value:") | |
for i, _ := range s { | |
fmt.Print(i, " ") | |
} | |
fmt.Println("") | |
fmt.Println("range index/value:") | |
for i, v := range s { | |
fmt.Print(i, "=", v, " ") | |
} | |
fmt.Println("") | |
fmt.Println("byte cast - range index only:") | |
for i := range []byte(s) { | |
fmt.Print(i, " ") | |
} | |
fmt.Println("") | |
fmt.Println("byte cast - range index/unused value:") | |
for i, _ := range []byte(s) { | |
fmt.Print(i, " ") | |
} | |
fmt.Println("") | |
fmt.Println("byte cast - range index/value:") | |
for i, v := range []byte(s) { | |
fmt.Print(i, "=", v, " ") | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment