Get the App
SLTechnology News&Howtos  ›  Development  › 

How to use the rune type in golang

Shulou Source: shulou.com Published: 2022-06-02 09:05:12 09月26日 Update

This article introduces the relevant knowledge of "how to use the rune type in golang". Many people will encounter such a dilemma in the operation of actual cases, so let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!

Rune types in golang

Rune is equivalent to int32 in golang, but is generally used for character conversion. The len () method in golang mainly calculates the length of the array.

The default storage string in golang is utf8 format, utf8 is variable-length byte storage, English letter is single-byte storage, Chinese is 3-byte storage, so the execution results of-1 and-2 are 16 and 15. There are two ways in golang, utf8.RuneCountInString and [] rune (), to convert utf8 into 4-byte int32 storage, and then calculate the length of the int32 array.

-1 address: = "this is shanghai" fmt.Println ("len (address):", len (address))-2 address: = "this is shanghai" fmt.Println ("len (address):", len (address))-3 addressThree: = "this is in Shanghai" fmt.Println ("len (address):", utf8.RuneCountInString (addressThree)-4 fmt.Println ("len (address):", len ([] rune (addressThree)-5 unicode.Is (unicode.Han) C) / / can judge whether the character is Chinese or not

Result

-1 len (address): 16-2len (address): 15-3len (address): 5-4len (address): 5 "how to use rune types in golang" ends here. Thank you for reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!

Tags: Storage bytes types characters content arrays more knowledge results length utility learning and then just dilemmas letters strings actual situation articles Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno MariaDB Huawei Docker Microsoft OPPO Reno