Menu Close

Go – How to print double quote in string in go

Here, We will see how to print double quote in string in go. It is very easy to print something to the standard output in Go. You have to just import fmt package and call Print(), Println() or Printf() function.

There are many ways to print double quote (“) in string in go which are also described with code example.

  • By using %q string formatter
  • By using Quote() function in strconv package
  • By using backslash (\)
  • Keep string within back quote (‘) and use double quote within string

By using %q string formatter:

package main

import (
  "fmt"
)


func main() {

  str := "hello go"

  fmt.Printf("%s \n", str)

  fmt.Printf("%q \n", str)

}

Output:

hello go
"hello go"

By using backslash (\):

package main

import (
  "fmt"
)

func main() {

  fmt.Println("Hello \"go\" golang")

}

Output:

Hello "go" golang

Keep string within back quote (`) and use double quote within string:

package main

import (
  "fmt"
)

func main() {

  fmt.Println(`Hello "go" golang`)

}

Output:

Hello "go" golang

By using Quote() function in strconv package:

package main

import (
  "fmt"
  "strconv"
)

func main() {

  str := "hello go"

  fmt.Println(str)

  str = strconv.Quote(str)

  fmt.Println(str)

}

Output:

hello go
"hello go"

To learn more about golang, Please refer given below link:

https://www.techieindoor.com/go-lang-tutorial/

References:

https://golang.org/doc/
https://golang.org/pkg/
Posted in golang, Miscellaneous

Leave a Reply

Your email address will not be published.

Contact Us