how to convert byte array to string in golang?

Hi Friends 👋,

Welcome To aGuideHub! ❤️

To convert byte array to string in golang, use the string() method it will convert your array to string and return the converted string.


Follow the below tutorial if you are struggling with installing GO in windows.

https://aguidehub.com/blog/how-to-install-golang-in-windows/


Today, I will show you how do i convert byte array to string in golang, as above mentioned I’m going to use the string() method.

To use the string() method, we have to pass the byte array as a parameter then the string method will convert and return the byte array as a string.

Let’s start our Golang convert byte array to string example

main.go

package main

import (
    "fmt"
)

func main() {
    byteArray := []byte{'a', 'G', 'u', 'i', 'd', 'e', 'H', 'u', 'b'}
    str1 := string(byteArray[:])
    fmt.Println("String =", str1)
}

Output

OUTPUT

String = aGuideHub
[T+0000ms]
Program exited.

Try it Yourself

I hope it helps you, All the best 👍.

Premium Content

You can get all the below premium content directly in your mail when you subscribe us

Books

Portfolio Template

View | Get Source Code

Cheat Sheets

Cheat Sheets Books are basically Important useful notes which we use in our day-to-day life.

I'm working on some more Cheat Sheets book on Jquery, TypeScript, React js and for other languages. I will send it once it's completed.

Stay tuned working on React Js Cheat Sheets Book

Related Posts