how to convert string to byte in golang?

Hi Friends 👋,

Welcome To aGuideHub! ❤️

To convert string to byte in golang, just use []byte and assign your value it will convert string to byte. Only you have to pass string into an byte.

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

Today, I will show you how do I convert string to byte in golang, as above mentioned I’m going to use []byte way.

Let’s start our Golang convert string to byte example

Convert whole string into an array example


package main
import "fmt"

func main() {
  s:= "infinitbility"
  b:= []byte(s)

In the above example, we have converted string to byte and printed in golang console. let’s check the output.


[105 110 102 105 110 105 116 98 105 108 105 116 121]

Try it Yourself

I hope it helps you, All the best 👍.