Golang Program To Convert An Array Into A String And Join Elements With A Specified Character


In this tutorial, we will write a go language program to convert an array to a string and separate them by a specified character. Arrays are data structures that are used to store values at contiguous memory locations.

Method 1: Convert an Array into a String using Join() Function

In this method, we will write a golang program to convert an array to a string using a library function. We will make a separate function, which will accept the array to be converted as an argument, and convert it to the string, and returns the result.

Syntax

func Join(s []string, sep string) string

The join function is used to convert an array to string. This function is present in the strings package. it takes two arguments first one is the array that we wish to convert and the second is the separation with which the array elements should be separated once they are converted to strings and returns the final string.

func typeofobject(x interface{})

The typeof() function is used to get the type of any variable. This function is present in reflect package and it takes the variable whose type is to be determined as an argument. The function then returns the type of the variable specified as the result.

Algorithm

Step 1 − First, we need to import the fmt, strings and reflect package.

Step 2 − Then, create a function to convert an array to string. This function accepts the array as an argument returns the result as string.

Step 3 − Start the main() function. Initialize an array and assign value to it print it on the screen along with the type of variable.

Step 4 − Now, call the function by passing the array as argument to the function and store the value returned by the function in a variable.

Step 5 − the function uses the join() library function to carry the respective conversion. At last we need to print the value of string on the screen along with its type by using fmt.Println() function.

Example

Golang program to convert an array into a string using join() function.

package main
import (
   "fmt"
   "reflect"
   "strings"
)

// function to convert an array to string
func arrayToString(arr []string) string {

   // seperating string elements with -
   return strings.Join([]string(arr), "-")
}
func main() {

   // initializing an array
   array := make([]string, 0, 3)
   array = append(array, "An", "Apple", "A", "Day", "Keeps", "Doctor", "Away")
   
   // printing array
   fmt.Println("The given array is:", array)
   fmt.Println("Its type is:", reflect.TypeOf(array))
   fmt.Println()
   result := arrayToString(array)
   fmt.Println("The value recieved is:\n", result, "\n Its type is:", reflect.TypeOf(result))
}

Output

The given array is: [An Apple A Day Keeps Doctor Away]
Its type is: []string
The value recieved is:
   An-Apple-A-Day-Keeps-Doctor-Away
   Its type is: string

Method 2: Convert an Array to a String by using the Sprint() Function

In this method, we will write a go language program to convert an array to a string by using sprint() function.

Syntax

func Sprint(format string, a ...interface{}) string

This function returns a formatted string. It takes a number of arguments in string format. The first argument should be a string format followed by a variable number of arguments.

func Split(str, sep string) []string

Split() function is used to split a string through a provided separator. This function is present in strings package and it accepts the string to split as an argument along with a separator. The function then returns the final array of strings as a result.

func Join(s []string, sep string) string

The join function is used to convert an array to string. This function is present in strings package. it takes two arguments first one is the array that we wish to convert and second is the separation with which the array elements should be separated once they are converted to strings and returns the final string.

Algorithm

Step 1 − First, we need to import the fmt, strings and reflect package.

Step 2 − Then, create a function to convert an array to string. This function accepts the array as an argument returns the result as string.

Step 3 − Start the main() function. Initialize an array and assign value to it print it on the screen along with the type of variable.

Step 4 − Now, call the function by passing the array as argument to the function and store the value returned by the function in a variable.

Step 5 − The function uses the sprint() library function to convert array to string and split function to split the string variables through the specified character.

Step 6 − At last we need to print the value of string on the screen along with its type by using fmt.Println() function.

Example

Golang program to convert an array to a string by using sprint() function.

package main
import (
   "fmt"
   "reflect"
   "strings"
)

// function to convert an array to string
func arrayToString(arr []string) string {
   
   // seperating string elements with -
   string := fmt.Sprint(arr)
   res := strings.Split(string, "-")
   return strings.Join(res, " ")
}
func main() {
   array := []string{"Hello", "world", "!"}
   fmt.Println("The given array is:", array)
   fmt.Println("Its type is:", reflect.TypeOf(array))
   fmt.Println()
   result := arrayToString(array)
   fmt.Println("The value recieved is:\n", result, "\n Its type is:", reflect.TypeOf(result))
}

Output

The given array is: [Hello world !]
Its type is: []string
The value recieved is:
   [Hello world !]
   Its type is: string

Conclusion

We have successfully compiled and executed a go language program to convert an array to a string and joined the elements of the string together with a specified character. We have used two programs in this case. In the first program, we are using the join() function present in the strings package and in the second program, we are using the sprint() function of the go language to carry out the respective conversions.

Updated on: 10-Jan-2023

5K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements