Back to questions
JuniorTheoryCommon
What happens when this program is built and run?
package main
import "fmt"
func main() {
s := "go"
s[0] = 'G'
fmt.Println(s)
}It does not compile
- Expecting
Go, as if a string were a byte array you can edit: Go strings are immutable, sos[0] = 'G'is rejected withcannot assign to s[0]. - Expecting a panic at run time: the compiler already knows
sis a string, so the error stops the build and the program never starts.
- How would you build the string
"Go"froms? - What does
s[0]return when you read it instead of writing to it?