如题所述
VARCHAR是MySQL中常用的数据类型,但其单位究竟是字符还是字节呢?
经过查阅,MySQL的 官方文档 给出了答案:
4.1及之后的版本,VARCHAR的单位是字符;4.1之前的版本,VARCHAR的单位是字节。(As of version 4.1 , MySQL interprets length specifications in character column definitions in character units . (Before MySQL 4.1, column lengths were interpreted in bytes .) This applies to CHAR, VARCHAR and the TEXT types.)
同时官方文档也提到,4.1及之后的版本以UTF-8作为预定义字符集。(VARCHAR is shorthand for CHARACTER VARYING. NATIONAL VARCHAR is the standard SQL way to define that a VARCHAR column should use some predefined character set. MySQL 4.1 and up uses UTF8 as this predefined character set .)
在 Go 语言中,是不能用 len 函数来统计字符串中的字符个数的,这是因为在 Go 中,字符串是以 UTF-8 为格式进行存储的,在字符串上调用 len 函数,取得的是字符串包含的 byte 的个数。
那么如何在Go语言中获取字符串中的字符个数呢?有下面几种方法:
测试发现速度最快的是 utf8.RuneCountInString()