Thursday, January 7, 2010

golang中的迭代

python中的这种功能:

for key, value in a.items(): print key, value

go language也有这种功能:

func main() {
    var infomation string = "中国比美国好多了.";
    fmt.Println( infomation );
    //是unicode吗? 答案:否
    fmt.Println( len( infomation ) );
    fmt.Printf("%c\n", infomation[0]);
    for index, value := range infomation {
        //%c与C中的%c不同      the character represented by the corresponding Unicode code point
        fmt.Printf("%d:%c ", index, value);
    }
}
输出結果:
中国比美国好多了.
25
ä
0:中 3:国 6:比 9:美 12:国 15:好 18:多 21:了 24:.

range一个string时, 为unicode code point, 而不是byte

No comments:

Post a Comment

Note: Only a member of this blog may post a comment.