string.length in go


package main
import "fmt"
func main() {
    words := "hello world"
    fmt.Println(len(words))
}

2 comments:

  1. This is length in bytes not runes.

    ReplyDelete
  2. To actually get the real length of the strings (in runes, not bytes), use this:

    package main
    import "fmt"
    import "unicode/utf8"
    func main() {
    words := "hello, world"
    fmt.Println(utf8.RuneCountInString(words))
    }

    ReplyDelete