關於go字串

關於go字串

其中在go裡字串的sizeof並非是2 byte 而是回傳16 bytes

因為Strings in Go are represented by reflect.StringHeader 這個回傳的是StringHeader的Size

如果string字串內容很多,很明顯就不是其真實大小,要去計算長度.

type StringHeader struct {
        Data uintptr 
        Len  int     
}

see unsafe.SizeOf() says any string takes 16 bytes, but how

ref
Note

java字符串是Unicode字符集,UTF-16编码 golang 字符串是utf8编码

在 Go 语言 中要想获取 字符串 长度有四种方法,分别为:使用 bytes.Count() 获取、使用 strings.Count() 获取、使用 len() 获取和使用 utf8.RuneCountInString() 获取。

不同字元具有不同的編碼格式 UNICODE把所有字元設定為2個位元組,UTF-8格式則把所有字元設定為1--3個位元組

golang有自己的預設判斷長度函式len() len()函式判斷字串長度的時候,是判斷字元的位元組數而不是字元長度

Last updated