Home  >  Article  >  Backend Development  >  golang rune a few bytes

golang rune a few bytes

(*-*)浩
(*-*)浩Original
2019-12-17 10:51:502835browse

golang rune a few bytes

rune is used to distinguish character values ​​​​and integer values ​​​​                                                                                                                                                                                                                                            Represents a UTF-8 character. When you need to process Chinese, Japanese or other compound characters, you need to use the rune type. The rune type is equivalent to the int32 type. byte is equivalent to int8, which is one byte in length, commonly used to process ascii characters

rune is equivalent to int32, which is 4 bytes in length, and is commonly used to process unicode or utf- 8 characters

##Example:

package main

import (
    "fmt"
)


func main() {
    str := "你好 world"
    fmt.Printf("len(str):%d\n", len(str)) //返回len(str):12
}
The return value is 12 because Chinese characters occupy 2 bytes under unicode and utf-8 encoding It occupies 3 bytes, and golang's default encoding is exactly utf-8.

The above is the detailed content of golang rune a few bytes. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn