Golang Program to demonstrate the escape sequence characters


In the go programming language, there are several types of special escape sequence characters. Usage of each one of them implements a different type of functionality. We shall discuss each one of them one by one in the upcoming examples.

Example 1

In this example, we will write a go language program to demonstrate the backlash (//) character. It is used to display a slash in the output of the program.

package main
import "fmt"

func main() {
	fmt.Println("Program to display a backslash using in the program")
}

Output

Program to display a backslash using in the program\

Example 2

In this example, we will write a go language program to demonstrate the ascii bell character. It is displayed by \a escape character literal.

package main
import "fmt"

func main() {
    fmt.Println("Go landuage program to display an ASCII bell (BEL): \a")
}

Output

Go landuage program to display an ASCII bell (BEL): 

Example 3

In this example, we will write a go language program to demonstrate the line feed. It is displayed by \n escape character literal and is used to start a new line. The string/numbers that are written after this character come in a new line.

package main
import "fmt"

func main() {
	fmt.Println("Go landuage program to show a line feed. \nThis string will be displayed in a new line.")
}

Output

Go landuage program to show a line feed. 
This string will be displayed in a new line.

Example 4

In this example, we will write a go language program to demonstrate the carriage return. It is displayed by \r escape character literal and is used to start a new line. The difference between line feed and carriage return is that this return character gives a small space in the starting character of the new line.

package main
import "fmt"

func main() {
	fmt.Println("Go languaage program to display carriage return (CR): \r This is second statement and appears in the new line.")
}

Output

Go languaage program to display carriage return (CR): 
 This is second statement and appears in the new line.

Example 5

In this example, we will write a go language program to demonstrate the Horizontal tab. It is displayed by \t escape character literal and is used to give a space after the data where it is displayed.

package main
import "fmt"

func main() {
	fmt.Println("Go language program to display ASCII horizontal tab (TAB): \n This is second statement and appears after a space")
}

Output

Go language program to display ASCII horizontal tab (TAB): 
 This is second statement and appears after a space

Example 6

In this example, we will write a go language program to demonstrate the double quotations. It is displayed by \” escape character literal and is used to encapsulate the data in the double quotations that is entered within it.

package main
import "fmt"

func main() {
   fmt.Println("Golang program to display Double quote string literal (∖"this will come in quotations ∖"): ")
}

Output

Golang program to display Double quote string literal ("this will come in quotations"):

Example 7

In this example, we will write a go language program to demonstrate Unicode escape sequence character. It is displayed by \u and is used to include hexadecimal digits within the program.

package main

import "fmt"

func main() {
	fmt.Printf("Go language program to display a Unicode code point (Heart): %c", '\u2665')
}

Output

Go language program to display a Unicode code point (Heart): ♥

Example 8

In this example, we will write a go language program to demonstrate hexadecimal escape sequence character. It is displayed by \x and is used to represent a character by its hexadecimal code.

package main
import "fmt"

func main() {
	fmt.Printf("Go language program to show Hexadecimal escape sequence character (Star): %c", '\x2a')
}

Output

Go language program to show Hexadecimal escape sequence character (Star): *

Conclusion

We have successfully compiled and executed a go language program to demonstrate the escape sequence characters along with examples. In this article we have shown the shown different escape sequence literal characters along with their usage.

Updated on: 13-Feb-2023

1K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements