TODO: Byte of that little thing go

Source: Internet
Author: User
This is a creation in Article, where the information may have evolved or changed.

TODO: Byte of that little thing go

(This article go version go1.7.3 darwin/amd64)

In Golang, the bottom string is made up of a byte array.

Fmt. Println (Len ("DSD Good"))

The length of the output is 6

Fmt. Println (Len (Rune (' Good ')))

The length of the output is 3

Fmt. Println (Len ([]rune ("Good 2s"))

The length of the output is 4

So if you use string to store Unicode, if you have Chinese (Chinese is made up of 3 bytes

), press the subscript is not accessible, because you can only get a byte. If you want to access Chinese, you still need to use the Rune slice, so you can press the subscript access.

UTF-8 (8-bit Unicode Transformation Format) is a variable-length character encoding for Unicode, also known as the Universal Code. Created by Ken Thompson in 1992. It is now standardized to RFC 3629. UTF-8 encodes Unicode characters in 1 to 4 bytes. Use on the Web page to display the Chinese simplified traditional and other languages (such as English, Japanese, Korean) on a unified page.

Why do you want to do UTF-8 transcoding? Very large languages directly support UTF-8, and some languages store strings into memory directly using UTF-8 encoding. UTF-8 is a universal solution that has been maintained for some time. For example, Golang language directly supports UTF-8.

Next, let's see how Golang handles UTF-8 transcoding,

Output:

Why FMT. Println (Strtobyte ("country")) output [11]? By first understanding that the byte to range is 0~256,22283 to 256 the remainder is 11.

In Golang This is how you use UTF-8, do you notice the details of the use?

Wxgzh:ludong86

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.