create a variable of type string in go

To create a variable of type string in Go, we use the var keyword followed by the variable name and the value of the string. Here is an example:

main.go
var greeting string = "Hello, world!"
38 chars
2 lines

In this example, we are declaring a variable named greeting of type string and assigning it a value of "Hello, world!".

We can also declare a variable without specifying its value. In this case, Go initializes the variable with a default value, which is an empty string.

main.go
var message string
19 chars
2 lines

In this case, we are declaring a variable named message of type string and not assigning any value to it.

related categories

gistlibby LogSnag