0

We can use "`" to not escape a string:

package main

import "fmt"

func main()  {
    fmt.Println(`abc\tdef`) // abc\tdef
}

But how to get or print a non-escaped string variable?

package main

import "fmt"

func main()  {
    s := "abc\tdef"
    fmt.Println(s) // abc def
}

1 Answer 1

5

Use %#v and Sprintf:

package main

import "fmt"

func main()  {
    s := "abc\tdef"
    s = fmt.Sprintf("%#v", s)
    fmt.Println(s) // "abc\tdef"
}

%#v: a Go-syntax representation of the value

Sprintf: Sprintf formats according to a format specifier and returns the resulting string.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.