create a mutable variable of type string in go

To create a mutable variable of type string in Go, use the var keyword followed by the variable name, the type string, and the empty string value "". The var keyword is used to declare global or package-level variables, as well as local variables within functions.

Here is an example:

main.go
package main

import "fmt"

func main() {
   var message string = ""
   message = "Hello, world!"
   fmt.Println(message)
}
124 chars
10 lines

In this code, we first declare the message variable as a string with the initial value of the empty string "". We then assign a new value of "Hello, world!" to the message variable. This demonstrates that the variable is mutable.

Output:

main.go
Hello, world!
14 chars
2 lines

gistlibby LogSnag