Home  >  Article  >  Backend Development  >  How to convert string to byte in golang

How to convert string to byte in golang

PHPz
PHPzOriginal
2023-03-29 11:28:144237browse

As Golang becomes more and more popular and widely used, people's demand for it is also increasing. Among them, strings and byte sets are two important data structures that are often encountered in Golang programming. In some applications, it is necessary to convert a string into bytes and output the bytes to a file or send over the network. Let's discuss the method of converting string to byte in Golang.

In Golang, strings and bytes are immutable. In practical applications, in many cases it is necessary to convert strings into bytes for processing. At this time, you can use the []byte type in Golang to convert the string into a Byte type slice for processing. The following is a sample code:

func str2byte() {
    str := "Hello, Golang"
    bytes := []byte(str)
    fmt.Printf("%v", bytes)
}

In the above code, we define a string variable str, convert it into a slice of Byte type and assign it to the bytes variable. Finally, use the fmt package to output the value of the bytes variable.

If you need to convert a string into a fixed-length Byte array, you can use the copy function in the Go language. The following is a sample code:

func str2byte() [10]byte {
    str := "Hello, Golang"
    var bytes [10]byte
    copy(bytes[:], str[:10])
    return bytes
}

In this sample code, we define a Byte array with a length of 10, and copy the first 10 characters of the string to the array through the copy function. It should be noted that if the string length is less than 10, only the characters of the corresponding length will be copied to the byte array.

In addition to using the copy function for conversion, we can also use the functions in the strconv package to convert strings into bytes. The strconv package provides conversion between bytes and various data types (such as integers, floating point, booleans, etc.). The following is the sample code:

func str2byte() []byte {
    str := "Hello, Golang"
    bytes := strconv.AppendQuote([]byte{}, str)
    return bytes
}

In this sample code, we call the AppendQuote function in the strconv package and assign its return value to the bytes variable. The AppendQuote function will quote the incoming string and return its byte slice form.

In the process of converting a string into a Byte array, if Unicode characters such as Chinese are involved, you need to pay attention to encoding issues. Because the number of bytes occupied by Chinese equal-width characters under UTF-8 encoding is not fixed. If you want to represent Chinese characters in a Byte array, you must convert it to UTF-8 encoding and then convert it.

In short, there are many ways to convert strings to Byte arrays in Golang. Developers can choose the most suitable method for conversion based on the actual application scenario. For different conversion methods and methods, you need to pay attention to encoding issues and array length issues to ensure the correctness and stability of the program. I hope the above content can help everyone understand and master the method of converting string to Byte array in Golang.

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