Go String Data Type

Gayathri. B

String Data Type in Go

  • In Go, the string data type is used to store sequences of characters, also known as text. String values must be enclosed in double quotes (").

Example:

package main
import "fmt"
func main() {
  var txt1 string = "Hello!"
  var txt2 string          // Declared but not initialized (defaults to empty string)
  txt3 := "World 1"        // Short variable declaration
  fmt.Printf("Type: %T, value: %v\n", txt1, txt1)
  fmt.Printf("Type: %T, value: %v\n", txt2, txt2)
  fmt.Printf("Type: %T, value: %v\n", txt3, txt3)
}

Output:

Type: string, value: Hello!
Type: string, value: 
Type: string, value: World 1
Note:
  • A string variable that is declared but not initialized will have the default value of an empty string ("").
  • You can use either the var keyword with a type or the shorthand := for declaring and initializing string
Tags
Our website uses cookies to enhance your experience. Learn More
Accept !

GocourseAI

close
send