rodriggj / Go

0 stars 0 forks source link

1.11 How to get the length of a utf-8 String #11

Open rodriggj opened 2 years ago

rodriggj commented 2 years ago

Example 1: len(string)

  1. Create a main.go file and enter the following code:
    
    package main

import "fmt"

func main() { name := "carl" fmt.Println(len(name)) }


2. Run the code 
```sh 
go run main.go 

Results in ...

image

NOTE: The return value is 4 as expected if you count the letters of the string "carl". But recognize 2 things: 1. The len(string) function only works on English strings. A foreign or non-utf8 sequence of characters will not return a count. 2. The count IS NOT the number of characters but in fact the byte count for the string.