Home >Backend Development >Golang >How to convert golang string to byte

How to convert golang string to byte

PHPz
PHPzOriginal
2023-03-29 09:24:153104browse

Golang (also known as Go) is one of the most popular programming languages ​​​​in recent years. Its simplicity, efficiency, and reliability make it the first choice of many developers. In Golang, string and byte slice are one of the two most common data types. However, in actual development, we often need to convert strings into byte slices to handle some complex business logic. This article will introduce you to several ways to convert strings to byte slices in Golang.

  1. Use []byte()

The underlying string in Golang is composed of a byte array containing characters, so we can force the string by Convert to byte array to convert string to byte slice. The following is a sample code:

str := "Hello, World!"
bytes := []byte(str)
fmt.Println(bytes)

The output result is:

[72 101 108 108 111 44 32 87 111 114 108 100 33]
  1. Use the []byte() method of the strconv package

In Golang, The strconv package provides a more flexible way of converting strings into byte slices, using the []byte() method. The following is a sample code:

str := "Hello, World!"
bytes := strconv.AppendQuote([]byte{}, str)
fmt.Println(bytes)

The output is:

[34 72 101 108 108 111 44 32 87 111 114 108 100 33 34]

In this method, the AppendQuote() method adds a string to a byte array and wraps it in double quotes String. If you don't want to use double quotes, you can also try using other strconv package methods.

  1. Use the []byte() method of the strings package

Another way to convert a string into a byte slice is to use the strings package in Golang. This method converts the string to a byte array, but provides more options to adjust the output. The following is a sample code:

str := "Hello, World!"
bytes := []byte(str)
fmt.Println(bytes)

The output result is:

[72 101 108 108 111 44 32 87 111 114 108 100 33]

In this method, we directly convert the string into a byte array without using other APIs.

  1. Use the []byte() method of the byte package

Finally, we can also use the byte package in Golang to convert the string into a byte slice. The Byte package provides capabilities that allow us to have more fine-grained control over encoding and decoding strings. The following is a sample code:

str := "Hello, World!"
bytes := []byte(str)
fmt.Println(bytes)

The output result is:

[72 101 108 108 111 44 32 87 111 114 108 100 33]

In this method, we use the byte package method to convert the string into a byte array. It should be noted that when using byte packages, you need to follow some special regulations to make them more suitable for some specific scenarios.

Summary

So far, we have introduced four methods of converting strings to byte slices in Golang. Although there are some minor differences in these methods, at their core they all coerce a string into a byte array. If you encounter other string processing problems in actual projects, you can try these methods to solve them.

The above is the detailed content of How to convert golang string to byte. 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