在Go中切片字符串:如何切片阿拉伯(或其他unicode)字符串?

I need to slice a string in Go. Sometimes I have latin chars, otherwise I have arabic chars, but [:1] for arabic returns a different value.

package main

import "fmt"

func main() {
    a := "a"
    fmt.Println(a[:1]) // work

    b := "ذ"
    fmt.Println(b[:1]) // not work
    fmt.Println(b[:2]) // work

    fmt.Println(len(a) == len(b)) // false
}

http://play.golang.org/p/R-JxaxbfNL

First of all, you should really read about strings, bytes and runes in Go.

And here is how you can achieve what you want: Go playground (I was not able to properly paste arabic symbols, but if Chinese works, arabic should work too).

    s := "abcdefghijklmnop" 
    fmt.Println(s[2:9]) 

    s = "维基百科:关于中文维基百科" 
    fmt.Println(string([]rune(s)[2:9]))

The output is:

cdefghi
百科:关于中文