Golang or Go for short is an incredibly modern and powerful programming language with exceptional features. Go is capable or providing high performance while still retaining security, reliability, and practicality of a language such as Python.

However, it does offer features that are outside the 'norm' of a programming language. An example is a byte.

In Go, a byte refers to 8-bit unsigned integer used to represent character values. Hence, Go does not offer a traditional string data type as you would find in Python, JavaScript, etc. Instead, it uses a byte and rune tyoes to represent string types.

We will be covering Go bytes and rune data types in upcoming tutorials. Stay tuned for that.

When you convert between a byte slice and a string, Go returns a new slice containing the similar bytes as the string and the reverse is true.

Method 1 - Convert Byte Array to String using Slice

The easiest and most common method of converting a byte array to a string is used a slice. Go allows us to pass a byte array to a string constructor using slicing and returns the output string.

The example code below illustrates this method works:

package main

import "fmt"

func main() {
	byteArr := []byte{'g', 'e', 'e', 'k', 'b', 'i', 't', 's'}
	str := string(byteArr[:])
	fmt.Println("result: ", str)
}

In the example above, we use slicing method to convert the given byte array to a string. Run the code above:

go run byte_array_to_string.go 
result:  geekbits

Method 2 - Convert Byte Array to String using Bytes Package

Go also provides us with the bytes package which allows us to convert a given Byte Array into a string using the NewBuffer() and String() function.

An example code is as shown:

package main

import (
	"bytes"
	"fmt"
)

func main() {
	byteArr := []byte{'g', 'e', 'e', 'k', 'b', 'i', 't', 's'}
	str := bytes.NewBuffer(byteArr).String()
	fmt.Println("result: ", str)
}

Running the code above should return the corresponding string as:

go run byte_array_to_string.go
result:  geekbits

Method 3 - Using the Sprintf() Function

If you are familiar with the Go fmt package, you may wonder where is the sprintf function. Yes, we can use this function to convert a given bytes slice to a corresponding string. In benchmark side of things, this function is regarded as slower compared to other outlined methods.

An example code is as shown:

package main

import "fmt"

func main() {
	byteArr := []byte{'g', 'e', 'e', 'k', 'b', 'i', 't', 's'}
	str := fmt.Sprintf("%s", byteArr)
	fmt.Println("result: ", str)
}

Running the program above should return the corresponding string as:

go run byte_array_to_string.go
result:  geekbits

Full Source Code

The following is the source code for all the examples outlined in this tutorial.

// Use slicing
package main

import "fmt"

	func main() {
		byteArr := []byte{'g', 'e', 'e', 'k', 'b', 'i', 't', 's'}
		str := string(byteArr[:])
		fmt.Println("result: ", str)
	}

// use bytes package and NewVBuffer method
package main

import (
	"bytes"
	"fmt"
)

func main() {
	byteArr := []byte{'g', 'e', 'e', 'k', 'b', 'i', 't', 's'}
	str := bytes.NewBuffer(byteArr).String()
	fmt.Println("result: ", str)
}

// use sprintf function
package main

import "fmt"

func main() {
	byteArr := []byte{'g', 'e', 'e', 'k', 'b', 'i', 't', 's'}
	str := fmt.Sprintf("%s", byteArr)
	fmt.Println("result: ", str)
}

Closing

In this post, we covered three main methods of converting a byte array into a string in the Go programming language. We hope you enjoyed and learned something from this post.

Leave us a comment down below and share.

See you in the next upcoming topics.

If you enjoy our content, please consider buying us a coffee to support our work:

Table of Contents
Great! Next, complete checkout for full access to GeekBits.
Welcome back! You've successfully signed in.
You've successfully subscribed to GeekBits.
Success! Your account is fully activated, you now have access to all content.
Success! Your billing info has been updated.
Your billing was not updated.