- 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
How to Return a String from the Function in Golang?
In programming, to make the code more modular, usable, and readable we break the code into different small blocks and these blocks are known as functions. A string is a data structure consisting of a set of alphabets. In solving any programming problem we want that the function should return some set of alphabets then we create a string in the function that the function returns in the end.
This tutorial will explain this concept with the help of two examples.
In the first example, we will define two strings and create a function that will return the string that is larger in length;
in the second example, we will create a function in which we will reverse the string and return it at the end of the function.
Method 1
Algorithm
Step 1: var stringVariable1, stringVariable2 string − Declare the string variables.
Step 2: stringVariable1 = "tutorialpoints" stringVariable2 = "tutorial" − Initialize the string variables.
longerStringVariable := longerString(stringVariable1, stringVariable2) − Calling the function with two parameters that will return the longer string and storing it into a variable.
if len(stringVariable1) >= len(stringVariable2) { } − Comparing both the strings passed in the parameter using len() function and returning the first string if it is longer.
else {} − returning the second string in the else condition.
Print the longer string at the end of the function.
Example
In this example, we are going to return the string that is larger in length. To achieve this we will use the len() function that returns the length of the variable that is passed in the function.
package main import ( // fmt package provides the function to print anything "fmt" ) // golang function with string parameter and string return type func longerString(stringVariable1, stringVariable2 string) string { //Comparing the length of both the strings if len(stringVariable1) >= len(stringVariable2) { // returning first string if it longer return stringVariable1 } else { // returning second string if it longer return stringVariable2 } } func main() { // declaring the string var stringVariable1, stringVariable2 string //Initializing the string stringVariable1 = "tutorialpoints" stringVariable2 = "tutorial" fmt.Println("Golang program to return a string from the function.") // passing argument in the function and storing the longer string in a variable longerStringVariable := longerString(stringVariable1, stringVariable2) fmt.Println("The longer string is", longerStringVariable) }
Output
Golang program to return a string from the function. The longer string is tutorialpoints
Algorithm
Step 1: var stringVariable string − Declare the string variable.
Step 2: string = "tutorialpoints" − Initialize the string variable.
Step 3: reverseStringVariable := reverseString(stringVariable) − Calling a function to reverse the string by passing a string variable to it and storing it into a variable.
runeArray := []rune(stringVariable) − Creating a rune from the string passed in the parameter.
○ for i := 0; i < len(stringVariable)/2; i++ {} − Running a for loop till half of the length of the string.
runeArray[i], runeArray[len(stringVariable)−i−1] = runeArray[len(stringVariable)−i−1], runeArray[i] − the concept we are using here is that we swapped the ith element with the len − i index element.
Returning the rune by converting it into the string.
Printing the reversed array.
Example
In this example, we will reverse the string using a for loop and return it at the end of the function.
package main import ( // fmt package provides the function to print anything "fmt" ) // golang function with string parameter and string return type func reverseString(stringVariable string) string { //Creating a rune from the string runeArray := []rune(stringVariable) // running a for loop over the rune from 0 till half // of the length of the string for i := 0; i < len(stringVariable)/2; i++ { // swapping the ith and len - i element runeArray[i], runeArray[len(stringVariable)-i-1] = runeArray[len(stringVariable)-i-1], runeArray[i] } return string(runeArray) } func main() { // declaring the string var stringVariable string // initializing the string stringVariable = "tutorialpoints" fmt.Println("Golang program to return a string from the function.") // passing argument in the function and storing the reversed string in a variable reverseStringVariable := reverseString(stringVariable) fmt.Println("The reversed string of", stringVariable, "is", reverseStringVariable) }
Output
Golang program to return a string from the function. The reversed string of tutorialpoints is stnioplairotut
Conclusion
These are the two examples of returning the string from a function where in the first example we have returned the string that is longer in length and in the second example we have returned the reverse of the string passed in the parameter. To learn more about go you can explore these tutorials.