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.go124 chars10 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.go14 chars2 lines
gistlibby LogSnag