
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
Write Double Quotes in a String in Golang
In Golang there are various techniques of printing double-quotes in the string. In this article, we are going to learn those techniques with various example.
Syntax
func Quote(s string) string
Quote() function is present in strconv() package and is used to add a given string literal in double quotes. The function accepts the string variable that is to be quoted as an argument and returns the string after adding double quotes to it.
func Sprintf(format string, a ...interface{}) string
This function returns a formatted string. It takes a number of arguments in string format. The first argument should be a string format followed by a variable number of arguments.
func Join(s []string, sep string) string
The join function is used to convert an array to string. This function is present in strings package. it takes two arguments first one is the array that we wish to convert and second is the separation with which the array elements should be separated once they are converted to strings and returns the final string.
Algorithm
Step 1 ? First, we need to import the fmt package.
Step 2 ? Start the main() function.
Step 3 ? Inside the main() initialize a variable of type string and assign value to it.
Step 4 ? Further print the variable on the screen.
Step 5 ? In order to induce this string in double quotes use the printf() function
Example 1
In this example, we will use the printf() along with %q format verb function in order to print the double quotes in a string.
package main import "fmt" func main() { var str string= "You must be the change you wish to see in the world." fmt.Println("The given string is:\n", str) fmt.Println() fmt.Printf("Printing the above string in double quotes as:") fmt.Println() fmt.Printf("%q\n", str) }
Output
The given string is: You must be the change you wish to see in the world. Printing the above string in double quotes as: "You must be the change you wish to see in the world."
Example 2
In this example, we will write a go language program to demonstrate the example to write a double quotes in a string by using Quote() function present in strconv package.
package main import ( "fmt" "strconv" ) func main() { var str string = "Never give up" fmt.Println("The given string is:\n", str) fmt.Println() var result string = strconv.Quote(str) fmt.Println("Printing the above string in double quotes as:\n", result) fmt.Println() }
Output
The given string is: Never give up Printing the above string in double quotes as: "Never give up"
Example 3
In this examplem we will write a go language program to demonstrate the example to write a double quotes in a string using back quotes. Using back quotes is another famous approach which is used to print the string along with quotations.
package main import ( "fmt" ) func main() { var str string = `This is a string with "double quotes" inside` fmt.Println("The given string is:\n", str) }
Output
The given string is: This is a string with "double quotes" inside
Example 4
In this article we will write a go language program to demonstrate the example to write a double quote in a string using sprint() function.
package main import ( "fmt" ) func main() { var str string = "Prevention is better than cure" fmt.Println("The given string is:\n", str) fmt.Println() var result string = fmt.Sprintf("%s", str) fmt.Println("The final string obtained after placing it to quotations is:\n", result) }
Output
The given string is: Prevention is better than cure The final string obtained after placing it to quotations is: "Prevention is better than cure"
Example 5
In this article we will write a go language program to demonstrate the example to write a double quote in a string using join() function.
package main import ( "fmt" "strings" ) func main() { var str string = "The way to get started is to quit talking and begin doing." fmt.Println("The given string is:\n", str) fmt.Println() var result string = strings.Join([]string{""", str, """}, "") fmt.Println("The final string obtained after placing it to quotations is:\n", result) }
Output
The given string is: The way to get started is to quit talking and begin doing. The final string obtained after placing it to quotations is: "The way to get started is to quit talking and begin doing."
Conclusion
We have successfully compiled and executed a go language program to demonstrate the example to write double quotes in a string along with examples. we have used different functions in this program to including sprint(), quote(), Join(), etc. in order to incorporate a given string literal in quotations.