Golang Program to to convert byte to string


March 19, 2022, Learn eTutorial
1402

For a better understanding of this example, we always recommend you to learn the basic topics of Golang programming listed below:

How to convert the byte to string

The process of converting a byte array to a string is called decoding. This procedure requires a charset. Hough, we should use charset for decoding a byte array. A charset or character set refers to the composite number of different characters that are being used and supportedby the computer hardware and software. 

How to convert the byte to string in the Go Program

Here we are showing how to convert the byte to string in the Go language. Here variable byteAr is holding the byte array to be converted. And variable str for holding the result string. Finally, the byte array is converted using string(byteAr) and print the result. Given below are the steps which are used in the Go program. 

ALGORITHM

STEP 1: Import the package fmt

STEP 2: Start function main()

STEP 3: Declare the byte array variable byteAr

STEP 4: Consider variable str for holding the result string

STEP 5: The byte array is converted using string(byteAr)

STEP 6: Finally print the variable str using fmt.Println()

 

Golang Source Code

                                          package main
import (
 "fmt"
)

func main() {

 byteAr := []byte{72, 101, 108, 108, 111, 32, 87, 111, 114, 108, 100}
 var str string
 str = string(byteAr)
 fmt.Println(str)
}
                                      

OUTPUT

Hello world