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 [Last updated : March 05, 2023]

Printing the double-quoted string in Golang

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 code to print double-quoted string

// 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!"




Comments and Discussions!

Load comments ↻






Copyright © 2024 www.includehelp.com. All rights reserved.