- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
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.
- Related Articles
- Swift Program to convert an array into a string and join elements with a specified character
- Python Program to convert an array into a string and join elements with a specified character
- Swift Program to convert the string into an array of characters based on the specified character
- Python Program to convert the string into an array of character
- Golang Program to convert a string into Uppercase
- Golang Program to convert a string into lowercase
- Golang Program to Convert Character to String
- Golang Program to Convert String to an Array
- Golang program to convert a linked list into an array and vice-versa
- Python Program To Convert An Array List Into A String And Viceversa
- Java program to convert a character array to string
- Golang Program to insert multiple elements into the array from the specified index
- Golang Program to check a string starts with a specified substring
- Golang program to convert array into slice
- Golang program to convert slice into array
