Explain what are string literals?

Learn the string literals in Go language, and types of string literals.
Submitted by IncludeHelp, on October 04, 2021

A string literal represents a string constant containing a sequence of characters. There are two types of strings literals,

  • Raw string literals:
    The raw string literals are character sequences enclosed within the backquotes (``).
  • Interpreted string literals:
    The interpreted string literals are character sequences enclosed within the double quotes ("").

Consider the below example – demonstrating the examples of raw and interpreted string literals

// Go program to demonstrate the
// examples Raw and Interpreted
// string literal

package main

import (
	"fmt"
)

func main() {
	// Raw string literal
	x := `Hello\tworld\nHow're you?`

	// Interpreted string literal
	y := "Hello\tworld\nHow're you?"

	fmt.Println("x :", x)
	fmt.Println("y :", y)
}

Output:

x : Hello\tworld\nHow're you?
y : Hello	world
How're you?

In the above program, variable x contains the raw string literal and variable y contains the interpreted raw string.

Golang FAQ »




Comments and Discussions!

Load comments ↻






Copyright © 2024 www.includehelp.com. All rights reserved.