Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to use the rune type in golang

2025-01-19 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

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!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report