How to use the strconv.Unquote() function in Golang
The strconv.Unquote() function
The strconv package’s Unquote() function is used to read the given string s as a single-quoted, double-quoted, or backquoted string and return the string value that s quotes.
Syntax
func Unquote(s string) (string, error)
Parameter
s: This is the value of the quoted string from which the string is obtained.
Return value
The Unquote() function returns the string value quoted by the specified string s.
Note: The
Unquote()function returns the equivalent one-character string if there is a single-quoted string.
Code
The following code shows how to implement the strconv.Unquote() function in Golang.
// using strconv.Unquote() Function in Golangpackage mainimport ("fmt""strconv")func main() {// declare variable s, err and assign value to its, err := strconv.Unquote("`Golang is one of my favourite programming languages`")// display resultfmt.Printf("The value for s: %q, and error: %v\n", s, err)// reassigning value to s, errs, err = strconv.Unquote("Golang is one of my favourite programming languages")// display resultfmt.Printf("The value for s: %q, and error: %v\n", s, err)}
Explanation
- Line 4: We add the
mainpackage. - Lines 6–9: We import the necessary packages for the Golang project.
- Lines 10–21: We define the
main()function. - Line 13: We define the variables
sanderr, pass a value to theUnquote()function, and assign it to the variables. - Line 20: We print the results.
Note: If the value of
sis syntactically valid, then the error will be<nil>.