How to convert golang byte to string

PHPz
Release: 2023-03-29 13:45:58
Original
2071 people have browsed it

Golang is an efficient programming language that provides rich libraries and tools to meet various needs. When you need to convert byte type data to string, Golang provides a variety of convenient methods. In this article, we will introduce some methods to implement Golang byte to string conversion, including standard library methods and other related extension libraries.

1. Standard library methods

Golang’s standard library provides many methods for converting byte arrays or slices into strings. The most common method is to use the string() function.

  1. Use the string() function to convert:

This method is very simple, just pass the byte array or slice into the string() function. The sample code is as follows:

package main

import "fmt"

func main() {
    byteData := []byte{'h', 'e', 'l', 'l', 'o'}
    strData := string(byteData)
    fmt.Println(strData)
}
Copy after login

The output result of this code block is: hello

  1. Use the ToString function in the bytes package for conversion:

bytes package The ToString() function is also provided, which can convert a byte array or slice into a string. Unlike the string() function, the ToString() function always returns the copied result and has no effect on the source data. The sample code is as follows:

package main

import (
    "bytes"
    "fmt"
)

func main() {
    byteData := []byte{'h', 'e', 'l', 'l', 'o'}
    strData := bytes.ToString(byteData)
    fmt.Println(strData)
}
Copy after login

The output result of this code block is: hello

  1. Use the Itoa() function in the strconv package for conversion:

When we need to convert byte type data into an integer represented by a string, we can use the strconv.Atoi() function to complete the conversion. The sample code is as follows:

package main

import (
    "fmt"
    "strconv"
)

func main() {
    byteData := []byte{'1', '2', '3', '4', '5'}
    intData, _ := strconv.Atoi(string(byteData))
    fmt.Println(intData)
}
Copy after login

The output result of this code block is: 12345

2. Extension library methods

In addition to the standard library, there are many extensions in the Golang community Library that provides other efficient methods for byte to string conversion. Here, we introduce two extension function libraries: Babelfish and Go-Conversion.

  1. Babelfish library

Babelfish is a multi-language translation library for Golang. It provides a variety of conversion methods, including byte to multiple string format conversions . Babelfish provides functions such as ToUTF8String(), ToAsciiString() and ToString(). We take ToUTF8String() as an example. The sample code is as follows:

package main

import (
    "fmt"
    "github.com/axgle/babel"
)

func main() {
    byteData := []byte{'h', 'e', 'l', 'l', 'o'}
    strData := babel.ToUTF8String(byteData)
    fmt.Println(strData)
}
Copy after login

The output result of this code block is: hello

  1. Go-Conversion Library

Go-Conversion is a tool library for converting between byte arrays and other types. It provides functions such as FromBytesToString(), FromHexToString() and FromBase64ToString() for converting byte arrays into string format. This article takes the FromBytesToString() function as an example. The sample code is as follows:

package main

import (
    "fmt"
    "github.com/radovskyb/go-packages/convert"
)

func main() {
    byteData := []byte{'h', 'e', 'l', 'l', 'o'}
    strData, err := convert.FromBytesToString(byteData)
    if err != nil {
        panic(err)
    }
    fmt.Println(strData)
}
Copy after login

The output result of this code block is: hello

Conclusion

Convert byte to string in Golang Very easy, using standard library functions is the simplest way. However, some extension libraries provide methods that provide more functionality and flexibility. Choose the most suitable method according to your needs. No matter which method is used, remember to consider the errors that may occur during conversion and perform appropriate error handling.

The above is the detailed content of How to convert golang byte to string. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template