ADVERTISEMENT
ADVERTISEMENT

How to print double-quoted string in Golang?

Golang | Printing double-quoted string: Here, we are going to learn how to print double-quoted string using fmt.Printf() in Go programming language?
Submitted by IncludeHelp, on August 04, 2021

Given a string, we have to print it within double-quotes.

When we want to print a string using the fmt.Printf function – we use "%s" format specifier. But, "%s" prints the string without double-quotes.

To print double-quoted string – we can use "%q" format specifier.

Consider the below example –

In this example, we are declaring a string variable str and printing it using "%s" and "%t" format specifier to understand the difference between printing the string with and without double quotes.

// Golang program to print double-quoted string

package main

import (
	"fmt"
)

func main() {
	str := "Hello, world!"

	fmt.Printf("value of str is: %s\n", str)
	fmt.Printf("value of str is: %q\n", str)
}

Output:

value of str is: Hello, world!
value of str is: "Hello, world!"

ADVERTISEMENT



ADVERTISEMENT



Comments and Discussions


ADVERTISEMENT

ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT

Languages: » C » C++ » C++ STL » Java » Data Structure » C#.Net » Android » Kotlin » SQL
Web Technologies: » PHP » Python » JavaScript » CSS » Ajax » Node.js » Web programming/HTML
Solved programs: » C » C++ » DS » Java » C#
Aptitude que. & ans.: » C » C++ » Java » DBMS
Interview que. & ans.: » C » Embedded C » Java » SEO » HR
CS Subjects: » CS Basics » O.S. » Networks » DBMS » Embedded Systems » Cloud Computing
» Machine learning » CS Organizations » Linux » DOS
More: » Articles » Puzzles » News/Updates

© https://www.includehelp.com some rights reserved.