golang len return type
package main
import (
"fmt"
)
func main() {
str := "Hello, World!"
length := len(str)
fmt.Println("Length of the string:", length)
}
Explanation:
package main
: This line declares that the current file belongs to themain
package, which is the entry point of the Go program.import "fmt"
: This imports thefmt
package, which provides formatting and printing functions, among other things.func main() { ... }
: This is the main function where the execution of the program begins.str := "Hello, World!"
: This line declares a variablestr
of type string and assigns it the value "Hello, World!".length := len(str)
: Thelen()
function in Go is used to find the length of strings, arrays, slices, maps, and channels. Here,len(str)
returns the length of the string stored in the variablestr
and assigns it to the variablelength
.fmt.Println("Length of the string:", length)
: This line prints the string "Length of the string:" followed by the value of thelength
variable to the standard output usingfmt.Println()
.