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 efficiently convert string and [] byte in Go

2025-02-27 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

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

Editor to share with you in Go string and [] byte how to transfer efficiently, I believe most people do not know much about it, so share this article for your reference, I hope you will learn a lot after reading this article, let's go to know it!

Preface

When we use go for data serialization or deserialization, we may often involve string and byte array conversions. For example:

If str, err: = json.Marshal (from); err! = nil {panic (err)} else {return string (str)}

Json is serialized to the [] byte type, which needs to be converted to a string type. When the amount of data is small, the overhead of conversion between types is negligible, but when the amount of data increases, it may become a performance bottleneck. Using efficient conversion methods can reduce this overhead.

Data structure

Before you know how to transform it, you need to understand its underlying data structure.

This article is based on go 1.13.12

String:

Type stringStruct struct {str unsafe.Pointer len int}

Slice:

Type slice struct {array unsafe.Pointer len int cap int}

Compared to the structure of slice, string lacks a cap field that represents capacity, so you can't use the built-in cap () function for string traversal, so why doesn't string need the cap field? Because string in go is designed as an immutable type (and, of course, in many other languages), because it cannot append elements like slice, it does not need the cap field to determine whether it exceeds the capacity of the underlying array to decide whether to expand.

Only the len attribute does not affect read operations such as for-range, because the for-range operation only decides whether to jump out of the loop based on the len

So why is the string set to immutable? Because this ensures that the underlying array of the string does not change.

For example, map uses string as the key, and if the underlying character array changes, the calculated hash value will also change, so that the previous value can not be found when positioning from map, so its immutable property avoids this situation, and string is also suitable as a key for map. In addition, immutability can also ensure the thread safety of data.

Conventional implementation

String immutability has many advantages, and in order to maintain its immutability, string and byte array interconversion is generally achieved by copying data:

Var a string = "hello world" var b [] byte = [] byte (a) / / string to [] bytea = string (b) / / [] byte to string

This approach is simple to implement, but is implemented through underlying data replication, which is converted to function calls to slicebytetostring and stringtoslicebyte respectively during compilation.

String to [] byte

Func stringtoslicebyte (buf * tmpBuf, s string) [] byte {var b [] byte if buf! = nil & & len (s)

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