
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Create Multi-Line String in Go using Q
In the Go programming language, strings are a built-in data type that represents sequences of characters. They are defined using double quotes (") and can contain any valid Unicode characters. The multi-line will be printed using backticks and the output will be printed on the console using fmt package. Let's see different examples to get a clear understanding of the concept.
The fmt employs the verb %q. To print the string in a format that can be securely included inside double-quoted string literals, use the printf function. This guarantees that any special characters, such as newlines, in the string will be correctly escaped.
Algorithm
Step 1 ? Create a package main and declare fmt(format package) package in the program where main produces executable Examples and fmt helps in formatting input and output.
Step 2 ? Create a main function and in that function make a multi_line_string variable and initialize it with a string with many lines.
Step 3 ? Using the %q verb to format the string such that it may be securely inserted inside double-quoted string literals, output the value of the multi_line_string variable using the fmt.Printf function.
Step 4 ? A format string and a list of arguments to format according to the format string are passed to the fmt.Printf method. The format string in this instance is "%q," and the parameter is multi_line_string. The string is printed using the %q verb.
Step 5 ? The print statement is executed using fmt.Println() function where ln means new line.
Example
In the following example we are going to use ?%Q with backticks in main function
package main import ( "fmt" ) func main() { multi_line_string := `This is //create a multiline string a multi-line string given here` fmt.Println("The output here is written as:") fmt.Printf("%q", multi_line_string) //print the string using %q verb }
Output
The output here is written as: "This is //create a multiline string\na multi-line\nstring given here"
Conclusion
We executed the program of creating a multi-line string using %Q. The verb is used in the example above.