Golang和UTF-8中的符文[关闭]

  1. What does "rune" mean?

  2. https://golang.org/pkg/unicode/utf8/#example_Valid Why is the answer is true in the first line?

  3. The function Valid accepts only an array?

  1. rune is an alias for the type int32. It is intended to make programs clear about the cases when an integer value represents a code point.
  2. Because the sequence of bytes 0x48, 0x65, 0x6c, 0x6c, 0x6f, 0x2c, 0x20, 0xe4, 0xb8, 0x96, 0xe7, 0x95, 0x8c (which is a representation of the utf-8 string Hello, 世界) is a valid UTF-8 sequence.
  3. While utf8.Valid accepts only array of bytes (there is no overloading in Go, so the "only" part is actually irrelevant), there are a few similar functions to check for validness: utf8.ValidRune and utf8.ValidString

I recommend reading an awesome article on the Go blog: Strings, bytes, runes and characters in Go, I believe after reading it and experimenting a bit you'll get answers to most of your utf8-related questions about Go.