String type not immutable, its holding second assign value

Hi, golang strings are immutable; I have below code, string at package scope level assigned, but reassigned into block level and its taking happily without any error or wrong; and reassigned 3rd time and it’s taking without issues/errors. I’m missing anything here!
package main

import “fmt”
var str11 string =“hello” //???
func main() {

str11="Hello, Go lang, Subbareddy jangalapalli"
fmt.Println(str11)
str11="Hello string is immutable, can't change"  //cant change or error; it should holds first value
fmt.Println(str11)
bs:=[]byte(str11)
fmt.Println("UTF-8:\n",bs)
fmt.Printf("%T\n",bs)

fmt.Println("CHAR\tUTF-8\tBINARY\tOCTAL\tHEX")
for i:=0; i<len(str11); i++{
	fmt.Printf("%c\t%v\t\t%b\t%o\t%#x\n",str11[i],str11[i],str11[i],str11[i],str11[i])

}

}

Thanks,
Subbareddy Jangalapalli

Reassigning is not mutating.

The string itself isn’t touched or changed, just the location the variable points to.

1 Like

got it, thank you

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.