Home »
Golang
Go String Data Type
Last Updated : May 19, 2025
String Data Type
In Go, the string data type is used to represent a string and to store a sequence of characters.
String data type creates a string which stores Unicode characters and are immutable in nature that means its contents cannot be changed.
Declare Variable of a String Type
You can declare a string variable by using the var
keyword followed by the variable name, the string
type, and assign a value enclosed in double quotes.
Example
In the following example, we declare a string variable to store a student's name:
package main
import "fmt"
func main() {
var studentName string = "Aarav Sharma"
fmt.Println("Student Name:", studentName)
}
When executed, this program outputs:
Student Name: Aarav Sharma
Shorthand Declaration of a String Type
String's type shorthand declaration and initialization can be done by using the :=
syntax. This will automatically define the type based on the assigned value (string).
Example
In the following example, we use shorthand syntax to declare and initialize a greeting message:
package main
import "fmt"
func main() {
message := "Namaste, welcome to Go programming!"
fmt.Println("Message:", message)
}
When executed, this program outputs:
Message: Namaste, welcome to Go programming!
Go String Data Type Exercise
Select the correct option to complete each statement about the string data type in Go.
- Which keyword is used to declare a string variable in Go?
- Which of the following is a correct shorthand declaration of a string?
- What is the output of the following code?
name := "Kavya"
fmt.Println("Name:", name)
Advertisement
Advertisement