> For the complete documentation index, see [llms.txt](https://minilabmemo.gitbook.io/golang-memo/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://minilabmemo.gitbook.io/golang-memo/basic/go-basic/unsafe.sizeof/guan-wu-go-zi-chuan.md).

# 關於go字串

### 關於go字串

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

因為Strings in Go are represented by [`reflect.StringHeader`](https://godoc.org/reflect#StringHeader) `這個回傳的是`StringHeader的Size

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

```go
type StringHeader struct {
        Data uintptr 
        Len  int     
}
```

see [unsafe.SizeOf() says any string takes 16 bytes, but how](https://stackoverflow.com/questions/65878177/unsafe-sizeof-says-any-string-takes-16-bytes-but-how)

| ref                                                                                              | Note                                                                                                                                          |
| ------------------------------------------------------------------------------------------------ | --------------------------------------------------------------------------------------------------------------------------------------------- |
| [golang java字符串长度（包含中文）而不是字节长度](https://blog.csdn.net/kingyc123456789/article/details/107858842) | <p>java字符串是Unicode字符集，UTF-16编码<br>golang 字符串是utf8编码</p>                                                                                       |
| [Go语言字符串长度](https://haicoder.net/golang/golang-string-length.html)                               | 在 Go 语言 中要想获取 字符串 长度有四种方法，分别为：使用 bytes.Count() 获取、使用 strings.Count() 获取、使用 len() 获取和使用 utf8.RuneCountInString() 获取。                           |
| [golang 獲取字串長度的案例](https://www.796t.com/article.php?id=201489)                                   | <p>不同字元具有不同的編碼格式<br>UNICODE把所有字元設定為2個位元組，UTF-8格式則把所有字元設定為1--3個位元組</p><p><br>golang有自己的預設判斷長度函式len()<br>len()函式判斷字串長度的時候，是判斷字元的位元組數而不是字元長度</p> |
