×

Golang Tutorial

Golang Reference

Golang Programs

Golang Practice

Golang Miscellaneous

How to print double-quoted string in Golang?

By IncludeHelp Last updated : October 05, 2024

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

Printing the double-quoted string in Golang

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.

Golang code to print double-quoted string

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


Comments and Discussions!

Load comments ↻





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