Progress

Add double Quotes to Go lang String Formatting

Add double Quotes to Go lang String Formatting

If we directly try to print any string in Go lang then it will give the output of string without quotes. It is possible to add double quotes to Go lang string and print it with quotes.

We can suppose a situation when we need to print any string with its escape characters then we need string conversion strconv package of Go programming language.

strFormatting.go
Copy
package main

import (
	"fmt"
	"strconv"
)

func main() {
	name := "Go lang is awesome. \nIt is fast."
	strName := strconv.Quote(name)
	fmt.Println(name)
	fmt.Println(strName)
}
Output
Go lang is awesome. 
It is fast.
"Go lang is awesome. \nIt is fast."

Now, you can see the difference in string formatting.

The first line is default printing which does not display any quotes in Output. This is the general behaviour that we should expect.

In situations when we need to add double quotes to Go lang string we can use strconv package to do the same.

Share the Post ;)

Related Posts