Rabi Siddique
190 words
1 minutes
Double, Single, and Back Quotes in Go
2024-06-12

In Go, you can use different types of quotes to define strings and characters. Here’s a detailed explanation:

Double Quotes#

Double quotes ("") are used to define string literals. Strings created with double quotes can contain any text, including escape sequences like \n (new line), \t (tab), etc.


package main

import "fmt"

func main() {
    str := "Hello, World!"
    fmt.Println(str) // Output: Hello, World!
}

Back Quotes#

Back quotes (``) are used to define raw string literals. Raw strings can span multiple lines and do not interpret escape sequences. This is useful for including text exactly as it is.

package main

import "fmt"

func main() {
    rawStr := `This is a raw string.
It can span multiple lines.
No escape sequences like \n or \t are interpreted.`
    fmt.Println(rawStr)
}

Single Quotes#

Single quotes ('') are used to define a single character, which can be a byte or a rune. If you don’t explicitly declare the type, it defaults to a rune, which is an alias for int32.


package main

import "fmt"

func main() {
    ch := 'a' // This is a rune
    fmt.Printf("Character: %c, Unicode: %U\n", ch, ch) // Output: Character: a, Unicode: U+0061
}
Double, Single, and Back Quotes in Go
https://rabisiddique.com/posts/quotes-in-golang/
Author
Rabi Siddique
Published at
2024-06-12