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 👍.

Premium Content

You can get all the below premium content directly in your mail when you subscribe us

Books

Interview Questions

Soon You will get CSS, JavaScript, React Js, and TypeScript So Subscribe to it.

Portfolio Template

View | Get Source Code

Cheat Sheets

Cheat Sheets Books are basically Important useful notes which we use in our day-to-day life.

Related Posts