Menu Close

Go – how to split string into characters in go golang

Here, We are going to learn about how to split string into characters in go golang.

So, there are many ways to do that.

By using Spilt() function of strings package:

package main

import (

  "fmt"
  "strings"

 )

func main(){

  var str = strings.Split("hello", "")

  fmt.Printf("%q", str)
}

Output:

 ["h" "e" "l" "l" "o"]

By using rune:

package main

import (
  "fmt"
 )

func main(){

  var str = "hello"

  r := []rune(str)

  for _, x := range(r) {

    fmt.Printf("%c ", x)

  }
}

Output:

 h e l l o

By using range() function:

package main

import (
  "fmt"
 )

func main(){

  var str = "hello"

  for _, x := range(str) {

    fmt.Printf("%c ", x)

  }
}

Output:

h e l l o

To learn more about golang, You can refer given below link:

https://techieindoor.com/go-lang-tutorial/

References:

 https://golang.org/doc/
https://golang.org/pkg/
https://golang.org/pkg/fmt/
https://golang.org/pkg/fmt/#Println
Posted in golang, Miscellaneous

Leave a Reply

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