Golang Program to find the length of the string


April 4, 2022, Learn eTutorial
1310

For a better understanding of this example, we always recommend you to learn the basic topics of Golang programming listed below:

How to find the length of the string

The length or the size of a string is the total number of characters present in the given string. For example the string “learnetutorials”  has 15 characters. Most of the programming languages have their own inbuilt method to calculate the length of a string.

How to find the length of the string in the GO Program

Here we are showing how to find the length of the string in the Go language. Here variable string is holding the string to find the length. And variable len for holding the length of the string. Finally, the string length is calculated as len(string) and print the result. Given below are the steps which are used in the Go program. 

ALGORITHM

STEP 1: Import the package fmt

STEP 2: Start function main()

STEP 3: Declare the string variable string

STEP 4: Assign string with the text "Hi how are you"

STEP 5: Find the string length as len(string)

STEP 6: Finally print the variable len using fmt.Println()

 

Golang Source Code

                                          package main

import (
    "fmt"
)

func main() {

    string := "Hi how are you"
    fmt.Println(string)

    len := len(string)
    fmt.Println("The length of a given string = ", len)
}
                                      

OUTPUT

The length of a given string = 14