In Go the type size of 1 byte can reach 1 kilobyte, even 1 terabyte or much more.

161 views
Skip to first unread message

Christian Maurer

unread,
Dec 10, 2020, 7:37:58 AM12/10/20
to golang-nuts

// Proof: Set n to a number >= 39 in the following program:

func main() {
  const (
    b = byte(0xc0)
    n = 9
  )
  s := []string{string(b)}
  for i := 0; i < n; i++ {
    s = append(s, []string{""}...)
  }
  for i := 0; i < n; i++ {
    for j := 0; j < len(s[i]); j++ {
      s[i+1] += string(s[i][j])
    }
  }
  println(len(s[n]), len(s[n]) == 1<<(n+1))
}

Marvin Renich

unread,
Dec 10, 2020, 8:22:25 AM12/10/20
to golang-nuts
* Christian Maurer <dr.ch....@gmail.com> [201210 07:37]:
Did you try adding

fmt.Printf("s[0] = %q len(s[0]) = %d\n", s[0], len(s[0]))

after the initial assignment to s? Strings are treated as UTF-8, and
string(byte(0xc0)) converts the rune (Unicode code point) '\u00c0' to
its UTF-8 representation as a string "\xc3\x80".

See https://golang.org/ref/spec#Conversions and scroll down to
"Conversions to and from a string type".

...Marvin

Reply all
Reply to author
Forward
0 new messages