To convert a string containing escape sequences to its unescaped form using strconv.Unquote()
in Golang, follow these steps:
import (
"fmt"
"strconv"
)
str := `"Hello\nWorld\t!"`
strconv.Unquote()
to unescape the string:unescapedStr, err := strconv.Unquote(str)
if err != nil {
fmt.Println(err)
return
}
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.