how to convert string to char array in golang?
September 18, 2022Hi Friends 👋,
Welcome To aGuideHub! ❤️
To convert string to char array in golang, just use Split()
method and pass your string with ""
empty seprator, it will convert string into char array.
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 char array in golang, as above mentioned I’m going to use Split()
way.
The strings
package provide Split()
method.
Let’s start our Golang convert string to char array example
Convert whole string into char array example
main.go
package main
import (
"fmt"
"strings"
)
func main(){
var str = strings.Split("aguidehub", "")
fmt.Printf("%q", str)
}
In the above example, we have converted string to char array and printed in golang console. let’s check the output.
Output
["a" "g" "u" "i" "d" "e" "h" "u" "b"]
I hope it helps you, All the best 👍.