
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Convert Set of Strings to Array of Strings in Golang
In this tutorial, we will write a golang program to convert a set of strings to array of strings. In go a set is an abstract data type that can store specific values without repeating any of them and without any particular sequence.
Convert Set Of String To Array Of Strings Using Internal Functions
The program to convert a set of strings to array of strings using predefined functions in go programming language is given below.
Syntax
func make([]type, length, capacity) []type
The make function is used to make a slice or map. It takes three arguments one is the slice name and type that we wish to make followed by the length and capacity of the slice. The function then returns the final slice.
func len(v Type) int
The len() function is used to get the length of a any parameter. It takes one parameter as the data type variable whose length we wish to find and returns the integer value which is the length of the variable.
Algorithm
Step 1 ? Import the fmt package.
Step 2 ? Now start the main function.
Step 3 ? Create a new set using the make() function and add values to it.
Step 4 ? Print the set on the screen using fmt.Println() function.
Step 5 ? Now create an array of string type.
Step 6 ? Store the length of the set in a variable of type int. use the for loop starting from 0 to the length of the set to iterate over the array.
Step 7 ? On each iteration store the value from the set to the array and repeat the process till the whole set is copied in the array.
Step 8 ? At last we need to print the array on the screen using fmt.Println() function.
Example
package main import ( "fmt" ) func main() { newset := make(map[int]string) newset[0] = "Sunday" newset[1] = "Monday" newset[2] = "Tuesday" fmt.Println("The obtained set is:", newset) var arr [3]string v := len(newset) for i := 0; i < v; i++ { arr[i] = newset[i] } fmt.Println("The array obtained from the set is:", arr) }
Output
The obtained set is: map[1:Monday 2:Tuesday 0:Sunday] The array obtained from the set is: [Sunday Monday Tuesday]
Convert Set Of Strings To Array Of Strings Using External Functions
Let us now write a go language program to convert a set of strings to array of strings using external functions.
Algorithm
Step 1 ? Import the fmt package.
Step 2 ? Make a function setToStrings() to convert the given set to array.
Step 3 ? Now start the main function.
Step 4 ? Create a new set using the make() function and add values to it.
Step 5 ? Print the set on the screen using fmt.Println() function.
Step 6 ? Call the setToString() function by passing the map variable as argument to the function.
Step 7? Now create an array of string type.
Step 8 ? Store the length of the set in a variable of type int. Use the for loop starting from 0 to the length of the set to iterate over the array.
Step 9 ? On each iteration store the value from the set to the array and repeat the process till the whole set is copied in the array.
Step 10 ? Return the array from the function and store it in a variable called output.
Step 11 ? At last we need to print the array on the screen using fmt.Println() function.
Example
package main import ( "fmt" ) func setToString(newset map[int]string) [3]string { var arr [3]string v := len(newset) for i := 0; i < v; i++ { arr[i] = newset[i] } return arr } func main() { newset := make(map[int]string) newset[0] = "Apple" newset[1] = "Mango" newset[2] = "Banana" fmt.Println("The obtained set is:", newset) output := setToString(newset) fmt.Println("The array obtained from the set is:", output) }
Output
The obtained set is: map[0:Apple 1:Mango 2:Banana] The array obtained from the set is: [Apple Mango Banana]
Conclusion
We have successfully compiled and executed a go language code to convert a set of strings to array of strings along with examples.