> For the complete documentation index, see [llms.txt](https://denglj.gitbook.io/essential-go/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://denglj.gitbook.io/essential-go/02-basic-types/characters-and-runes.md).

# 字符和符号(rune)

Go有两种字符类型：

* `byte`是长度为1字节的值，它是`uint8`类型的别名
* `rune`是长度为4字节的Unicode码位(Code-Point)，它是`int32`的别名

`byte`和`rune`的[零值](/essential-go/02-basic-types/ling-zhi.md)是0。

### 使用byte遍历字符串

{% tabs %}
{% tab title="Go" %}

```go
package main

import (
	"fmt"
)

func main() {
	s := "str"
	for i := 0; i < len(s); i++ {
		c := s[i]
		fmt.Printf("Byte at index %d is '%c' (0x%x)\n", i, c, c)
	}
}
```

{% endtab %}

{% tab title="Output" %}

```
Byte at index 0 is 's' (0x73)
Byte at index 1 is 't' (0x74)
Byte at index 2 is 'r' (0x72)
```

{% endtab %}
{% endtabs %}

:point\_right: [点击此处](https://play.golang.org/p/piIqE89sRRp) :point\_left: 在编程操场执行上述代码。

### 使用rune遍历字符串

{% tabs %}
{% tab title="Go" %}

```go
package main

import (
	"fmt"
)

func main() {
	s := "中国汉字"
	for i, runeChar := range s {
		fmt.Printf("Rune at byte position %d is %#U\n", i, runeChar)
	}
}
```

{% endtab %}

{% tab title="Output" %}

```
Rune at byte position 0 is U+4E2D '中'
Rune at byte position 3 is U+56FD '国'
Rune at byte position 6 is U+6C49 '汉'
Rune at byte position 9 is U+5B57 '字'
```

{% endtab %}
{% endtabs %}

:point\_right: [点击此处](https://play.golang.org/p/8T1qwnAsMfF) :point\_left: 在编程操场执行上述代码。

一个字符串本质上是一个字节数组。

当用`rune`遍历字符串时，编译器会假定待遍历的是用UTF-8编码的Unicode字符串。

UTF-8是一种变长编码方案，被编码的每个字符的长度可能是1,2,3或4字节。**这样遍历时，返回的变量`i`表示每个字符起始字节的位置，而不是字符所在串中第几个位置。**
