Golang Program to Convert Character to String


In this article, we are going to learn about how to convert characters to string go programming language.

Chars − Go language does not have a char data type on the contrary characters in go language are represented by rune data type. Rune represents a character value that is encoded in UTF-8 format. The size of the runes is 32-bits.

Strings − String data type is used to store a sequence of characters. it can be in the form of literals or alphabets. The size of the string variable is 1 byte or 8 bits.

There are two approaches to achieving this result. We shall discuss them in this article.

Syntax

func QuoteRune(r rune) string
func string(n int) string

The function QuoteRune() is defined in strconv package and is used to convert rune or character value to a string. This function takes the rune value as an input and converts it to return the output as a string.

Algorithm

  • STEP 1 − Import the required packages naming fmt, strconv and reflect.

  • STEP 2 − Start the main() function.

  • STEP 3 − Initialize a variable of type rune and assign value to it.

  • STEP 4 − Call the QuoteRune() method and pass the rune variable as the argument to it.

  • STEP 5 − Store the result obtained on a different variable of type string.

  • STEP 6 − Print the result on the screen using fmt.Println() function along with the type of the variable.

Example 1

Golang program to convert characters to string using QuoteRune() method.

package main

import (
   "fmt"
   "reflect"
   "strconv"
)
// fmt package allows us to print anything on the screen.
// strconv package allows us to use other pre-defined functions like QuoteRune().
// reflect package allows us to use methods that allow knowing the type of a variable.

// calling the main function
func main() {

   // initializing and defining a rune variable and assigning a value to it.
   var r rune = '☺'

   // using the QuoteRune() function defined in strconv package to convert runr to
      // string.
   // storing the string obtained in a new variable
   var s string = strconv.QuoteRune(r)

   // printing the type and value of the rune character
   fmt.Println("The given variable is of type rune and has value of", r)

   // printing the type and value of the string obtained from the rune.
   fmt.Println("The obtained variable is of type", reflect.TypeOf(s), "and has value of", s)
}

Output

The given variable is of type rune and has value of 9786
The obtained variable is of type string and has value of '☺'

Description

  • First we need to import the fmt, strconv, and reflect packages. fmt package allows us to print anything on the screen, the strconv package allows us to use other predefined functions like QuoteRune(), and reflect package allows us to use methods that allow knowing the type of a variable.

  • Call the main() function this is the starting point of our program.

  • Now initialize a variable of type rune and assign a value to it.

  • Next we need to call a method defined in the strconv package that will allow us to use the QuoteRune() method to pass the rune variable as the argument to this function and this function will return the corresponding string equivalent of the rune after converting it to string.

  • Now store the result obtained in a separate variable of type string.

  • Print the rune value on the screen using fmt.Println() function and on the next line print the string equivalent obtained along with its type.

Example 2

Golang program to convert character to string using String() function.

package main

import (
   "fmt"
   "reflect"
)
// fmt package allows us to print anything on the screen.
// reflect package allows us to use methods that allow knowing the type of a variable.

func runesToString(runes []rune) (outString string) {
   // don't need index so _
   for _, v := range runes {
      outString += string(v)
   }
   return
}

// calling the main funciton
func main() {

   // initializing and defining a rune variable and assigning a value to it.
   var r []rune = []rune{'a', 'b', 'z'}

   // Calling the runesToString() function to convert array of runes to string.
   // storing the string obtained in a new variable
   var s string = runesToString(r)

   // printing the type and value of the rune character
   fmt.Println("The given variable is of type rune and has value of", r)

   // printing the type and value of the string obtained from the rune.
   fmt.Println("The obtained variable is of type", reflect.TypeOf(s), "and has value of", s)
}

Output

The given variable is of type rune and has value of [97 98 122]
The obtained variable is of type string and has value of abz

Description

  • First we need to import the fmt, strconv and reflect packages. fmt package allows us to print anything on the screen, reflect package allows us to use methods that allow knowing the type of a variable.

  • Initialize a RuneToString() function to convert the array of runes to the string. This function will take an array that has runes in it as an argument and will return the corresponding string equivalent of it after successfully converting it.

  • Call the main() function this is the starting point of our program.

  • Now initialize an array of type runes and assign a value to it.

  • Next we need to call the RuneToString() method.

  • Now store the result obtained in a separate variable.

  • Print the rune value on the screen using fmt.Println() function and on the next line print the string equivalent obtained along with its type.

Updated on: 22-Dec-2022

3K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements