How to pass a string to the function in Golang?


In this tutorial, we are going to learn about how to pass a string in function as an argument 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. If you have created a string of alphabets and you want to perform some operations on the string then you need to pass that string to the function by passing it as an argument.

This tutorial will explain this concept with the help of two examples.

  • In the first example, we will pass the string and print all the vowels present in the string. A, E, I, O, and U are the vowels in the English alphabet.

  • In the second example, we are going to convert the lowercase string to uppercase and print in the function to which we have passed the string. For example, we have a string ball where all the alphabets are lower case the upper case of this string will be BALL. To do this task we are going to use the concept of ASCII values. ASCII value is nothing but a unique number assigned to all the alphabets, numbers, and special characters.

Syntax

The syntax to declare a function with a string as a parameter is.

func functionName(stringVariableName string) returnType {
	// logic
}

Algorithm

  • Step 1: var stringVariable string − Declare the string.

  • Step 2: string = “tutorialpoints” −Initialize the string

  • Step 3: printInVowels(stringVariable) − Call the function to which we are passing the string as an argument.

    • for i := 0; i < len(stringVariable); i++ { } − Running a for loop over the string.

    • if stringVariable[i] == ‘a’ || stringVariable[i] == ‘e’ || stringVariable[i] == ‘i’ ||

      stringVariable[i] == ‘o’ || stringVariable[i] == ‘u’ || stringVariable[i] == ‘A’ ||

      stringVariable[i] == ‘E’ || stringVariable[i] == ‘I’ || stringVariable[i] == ‘O’ ||

      stringVariable[i] == ‘U’ {} − Comparing the current character with the vowels.

  • Step 4: Print all the vowels present in the string in the called function.

Example

In this example, we are going to create a string variable, and then print all the vowels by passing the string to a printVowels() function.

package main

import (
    // fmt package provides the function to print anything
    "fmt"
)

// stringVariable is the argument in this function
func printVowels(stringVariable string) {
    fmt.Println("Printing all the vowels present in the string.")

    // running for loop over the string
    for i := 0; i < len(stringVariable); i++ {
        // using the if block we are checking that the current character is
        // vowel or not by comparing with all the vowels character
        if stringVariable[i] == 'a' || stringVariable[i] == 'e' || stringVariable[i] == 'i' || stringVariable[i] == 'o' || stringVariable[i] == 'u' ||
            stringVariable[i] == 'A' || stringVariable[i] == 'E' || stringVariable[i] == 'I' || stringVariable[i] == 'O' || stringVariable[i] == 'U' {
            fmt.Printf("The character %c at index %d is a vowel.\n ", stringVariable[i], i)
        }

    }
    fmt.Println()
}

func main() {
    // declaring the string
    var stringVariable string

    // initializing the string
    stringVariable = "tutorialpoints"

    fmt.Println("Golang program to pass a string in the argument.")
    // passing argument in the function
    printVowels(stringVariable)

}

Output

Golang program to pass a string in the argument.
Printing all the vowels present in the string.
The character u at index 1 is a vowel.
The character o at index 3 is a vowel.
The character i at index 5 is a vowel.
The character a at index 6 is a vowel.
The character o at index 9 is a vowel.
The character i at index 10 is a vowel.

Algorithm

  • Step 1: var stringVariable string − Declare the string.

  • Step 2: string = “tutorialpoints” − Initialize the string.

  • Step 3: upperCase(stringVariable) − Call the function to which we are passing the string as an argument.

    • stringVariable = strings.ToUpper(stringVariable) − Call the ToUpper() function present in the strings package and convert the string to upper case

  • Step 4: Print the string after converting all the characters of the string to uppercase.

Example

In this example, we are going to convert a string from lowercase to upper case by passing the string to a function.

package main

import (
    // fmt package provides the function to print anything
    "fmt"
    // strings package provides function related to string
    "strings"
)

// stringVariable is the argument in this function
func upperCase(stringVariable string) {
    fmt.Println("Converting lower case string to upper case.")

    // converting lower case to uppercase by calling ToUpper()
    // function from stings package
    stringVariable = strings.ToUpper(stringVariable)

    fmt.Println(stringVariable)
}

func main() {
    // declaring the string
    var stringVariable string

    // initializing the string
    stringVariable = "tutorialpoints"

    fmt.Println("Golang program to pass a string in the argument.")
    // passing argument in the function
    upperCase(stringVariable)

}

Output

Golang program to pass a string in the argument.
Converting lower case string to upper case.
TUTORIALPOINTS

Conclusion

These are the two examples of passing a string to the function where in the first one we have printed all the vowels present in the string. In the second one, we passed a string and converted it into upper−case characters. To learn more about Go you can explore these tutorials.

Updated on: 10-Jul-2023

687 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements