In Go, the string data type is used to store sequences of characters, often referred to as text. String values must always be enclosed within double quotation marks.
Example: Declaring and Using Strings
The following example demonstrates how to declare and initialize string variables, including assigning values at the time of declaration, leaving them uninitialized, and using shorthand syntax.
Go
x
package main
import ("fmt")
func main() {
var ducati string = "Speed king!"
var honda string
yamaha := "Rev your heart!"
fmt.Printf("Type: %T, value: %v\n", ducati, ducati)
fmt.Printf("Type: %T, value: %v\n", honda, honda)
fmt.Printf("Type: %T, value: %v\n", yamaha, yamaha)
}
Output:
The output of the above program will display the type and value of each string variable:
Go
Type: string, value: Speed king!
Type: string, value:
Type: string, value: Rev your heart!