Indexing a string
indexes its bytes (in UTF-8 encoding - this is how Go stores strings in memory), but you want to test the first character.
So you should get the first rune
and not the first byte
. For efficiency you may use utf8.DecodeRuneInString()
which only decodes the first rune
. If you need all the runes of the string
, you may use type conversion like all := []rune("I'm a string")
.
See this example:
for _, s := range []string{"asdf", ".asdf", "”asdf"} {
c, _ := utf8.DecodeRuneInString(s)
if c != '.' && c != ',' && c != '?' && c != '“' && c != '”' {
fmt.Println("Ok:", s)
} else {
fmt.Println("Not ok:", s)
}
}
Output (try it on the Go Playground):
Ok: asdf
Not ok: .asdf
Not ok: ”asdf