Go Programming Language String Length

Go Programming Language String Length

In Go programming, len function is used to find the length of the string.

length will be return as integer data type when using len function.

package main                                                               	 
                                                                           	 
import "fmt"                                                               	 
                                                                           	 
func main() {                                                              	 
	var str string    
Var length int                                                     	 
	str = "codingpointer.com!"                                             	 
                                                                           	 
	length = len(str)                                                     	 
	fmt.Printf("String Length '%s': %d\n", str, length)                    	 
} 
Output:
$ go build string-length.go
$ ./string-length
String Length 'codingpointer.com!': 18

length variable does not required to be declared before assigning len function result. We can also := to assign length as integer.

package main                                                               	 
                                                                           	 
import "fmt"                                                               	 
                                                                           	 
func main() {                                                              	 
	var str string                                                         	 
	str = "codingpointer.com!"                                             	 
                                                                           	 
	length := len(str)                                                     	 
	fmt.Printf("String Length '%s': %d\n", str, length)                    	 
} 
Output:
$ go build string-length.go
$ ./string-length
String Length 'codingpointer.com!': 18