Skip to content

String to Byte Array in Golang [Easy Way]

Simplest way to convert a string to a slice of bytes:

[]byte()

A set of examples to convert string to []byte:

package main

import "fmt"

func main() {

	stringVariable := "Here is a string...."

	// use a string variable
	byteSlice := []byte(stringVariable)

	// direct string
	byteSlice = []byte("Here is a string....")

	fmt.Println(string(byteSlice))
}

Leave a Reply

Your email address will not be published. Required fields are marked *