0
0
GoHow-ToBeginner · 2 min read

Go How to Convert Byte Slice to String - Simple Example

In Go, convert a byte slice to a string by using string(yourByteSlice), which creates a new string from the bytes.
📋

Examples

Input[72, 101, 108, 108, 111]
OutputHello
Input[]byte{87, 111, 114, 108, 100}
OutputWorld
Input[]byte{}
Output
🧠

How to Think About It

To convert a byte slice to a string in Go, think of the byte slice as a list of characters stored as numbers. You just need to tell Go to treat those bytes as text by using the string conversion, which creates a readable string from the byte values.
📐

Algorithm

1
Get the byte slice input.
2
Use the <code>string</code> conversion to turn the byte slice into a string.
3
Return or print the resulting string.
💻

Code

go
package main

import "fmt"

func main() {
    bytes := []byte{72, 101, 108, 108, 111}
    str := string(bytes)
    fmt.Println(str)
}
Output
Hello
🔍

Dry Run

Let's trace converting []byte{72, 101, 108, 108, 111} to string

1

Start with byte slice

bytes = [72 101 108 108 111]

2

Convert bytes to string

str = string(bytes) -> "Hello"

3

Print the string

Output: Hello

StepByte ValueCharacter
172H
2101e
3108l
4108l
5111o
💡

Why This Works

Step 1: Byte slice holds ASCII codes

Each number in the byte slice represents an ASCII code for a character.

Step 2: string conversion creates text

Using string(bytes) tells Go to make a string from those ASCII codes.

Step 3: Result is readable string

The output is a normal string you can print or use like any other text.

🔄

Alternative Approaches

Using bytes.Buffer
go
package main

import (
    "bytes"
    "fmt"
)

func main() {
    bytesSlice := []byte{72, 101, 108, 108, 111}
    var buffer bytes.Buffer
    buffer.Write(bytesSlice)
    str := buffer.String()
    fmt.Println(str)
}
This method is useful when building strings from multiple byte slices but is more verbose than direct conversion.
Using strings.Builder with string conversion
go
package main

import (
    "fmt"
    "strings"
)

func main() {
    bytesSlice := []byte{87, 111, 114, 108, 100}
    var builder strings.Builder
    builder.WriteString(string(bytesSlice))
    str := builder.String()
    fmt.Println(str)
}
Good for efficient string concatenation but requires converting bytes to string first.

Complexity: O(n) time, O(n) space

Time Complexity

Converting a byte slice to string requires copying each byte once, so it takes linear time proportional to the slice length.

Space Complexity

A new string is created, so space used is proportional to the number of bytes converted.

Which Approach is Fastest?

Direct string() conversion is fastest and simplest; buffer or builder methods add overhead and are better for complex string building.

ApproachTimeSpaceBest For
Direct string conversionO(n)O(n)Simple, fast conversion
bytes.BufferO(n)O(n)Building strings from multiple byte slices
strings.BuilderO(n)O(n)Efficient concatenation of strings
💡
Use string(yourByteSlice) for a quick and simple conversion from bytes to string in Go.
⚠️
Trying to print a byte slice directly without conversion prints byte values, not readable text.