To get the length of a given string in Go (Golang), you can use the len
function.
In this example,
str1
with a string value of "Hello World"
.len
function to get the length of the string and store the returned value in the variable length1
.len
function returns the length of the string.length1
to the console using fmt.Println
.// Length of "Hello World" String
package main
import (
"fmt"
)
func main() {
str1 := "Hello World"
length1 := len(str1)
fmt.Println("Length of given string is:", length1)
}
Length of given string is: 11
In this example,
str2
with a string value of "apple"
.len
function to get the length of the string and store the returned value in the variable length2
.len
function returns the length of the string.length2
to the console using fmt.Println
.// Length of "apple" String
package main
import (
"fmt"
)
func main() {
str2 := "apple"
length2 := len(str2)
fmt.Println("Length of given string is:", length2)
}
Length of given string is: 5
In this tutorial, we learned How to get Length of String in Go language with well detailed examples.