How to convert a string containing escape sequences to its unescaped form using strconv.Unquote() in Golang?

To convert a string containing escape sequences to its unescaped form using strconv.Unquote() in Golang, follow these steps:

  1. Import the required packages:
import ( "fmt" "strconv" )
  1. Define the string containing escape sequences:
str := `"Hello\nWorld\t!"`
  1. Use strconv.Unquote() to unescape the string:
unescapedStr, err := strconv.Unquote(str) if err != nil { fmt.Println(err) return }
  1. Print the unescaped string:
fmt.Println(unescapedStr)

Here's the complete example:

package main import ( "fmt" "strconv" ) func main() { str := `"Hello\nWorld\t!"` unescapedStr, err := strconv.Unquote(str) if err != nil { fmt.Println(err) return } fmt.Println(unescapedStr) }

Output:

Hello World !

In the example above, the original string str contains escape sequences \n and \t for newline and tab characters respectively. By using strconv.Unquote(), these escape sequences are converted to their unescaped form, resulting in the output "Hello\nWorld\t!", where \n is replaced by a newline character and \t is replaced by a tab character.