I'm just wondering why these asian characters in this string wont show up when I reverse and print the individual characters in the string.
package main
import "fmt"
func main() {
a := "The quick brown 狐 jumped over the lazy 犬"
var lenght int = len(a) - 1
for ; lenght > -1; lenght-- {
fmt.Printf("%c", a[lenght])
}
fmt.Println()
}
You are accessing the string array by byte not by 'logical character' To better understand this example breaks the string first as an array of runes and then prints the rune backwards.
http://play.golang.org/p/bzbo7k6WZT
package main
import "fmt"
func main() {
msg := "The quick brown 狐 jumped over the lazy 犬"
elements := make([]rune, 0)
for _, rune := range msg {
elements = append(elements, rune)
}
for i := len(elements) - 1; i >= 0; i-- {
fmt.Println(string(elements[i]))
}
}
Shorter Version: http://play.golang.org/p/PYsduB4Rgq
package main
import "fmt"
func main() {
msg := "The quick brown 狐 jumped over the lazy 犬"
elements := []rune(msg)
for i := len(elements) - 1; i >= 0; i-- {
fmt.Println(string(elements[i]))
}
}