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.

https://aguidehub.com/blog/how-to-install-golang-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

main.go

package main
import "fmt"

func main() {
  s:= "infinitbility"
  
  b:= []byte(s)
	fmt.Println(b)
	fmt.Println(string(b))
}

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

Output

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

Try it Yourself

I hope it helps you, All the best 👍.