Golang strconv.Unquote() Function with Examples

Golang | strconv.Unquote() Function: Here, we are going to learn about the Unquote() function of the strconv package with its usages, syntax, and examples.
Submitted by IncludeHelp, on September 10, 2021

strconv.Unquote()

The Unquote() function is an inbuilt function of the strconv package which is used to interpret the given string s as a single-quoted, double-quoted, or backquoted,  and returns the string value that s quotes. (If there is a single-quoted string, the function returns the corresponding one-character string.)

It accepts a parameter (s) and returns the string value that s quotes.

Syntax:

func Unquote(s string) (string, error)

Parameter(s):

  • s : The quoted string value from which we have to get the string.

Return Value:

The return type of the Unquote() function is a (string, error), it returns the string value that the given string (s) quotes.

Example 1:

// Golang program to demonstrate the
// example of strconv.Unquote() Function

package main

import (
	"fmt"
	"strconv"
)

func main() {
	fmt.Println(strconv.Unquote("Hello, world! How are you?"))
	fmt.Println(strconv.Unquote("\"Hello, world! How are you?\""))
	fmt.Println(strconv.Unquote("`Hello, world! How are you?`"))
	fmt.Println(strconv.Unquote("'\u2639'"))
}

Output:

 invalid syntax
Hello, world! How are you? <nil>
Hello, world! How are you? <nil>
☹ <nil>

Example 2:

// Golang program to demonstrate the
// example of strconv.Unquote() Function

package main

import (
	"fmt"
	"strconv"
)

func main() {
	str, err := strconv.Unquote("Hello, world!")
	fmt.Printf("%q, %v\n", str, err)

	str, err = strconv.Unquote("\"Hello, world!\"")
	fmt.Printf("%q, %v\n", str, err)

	str, err = strconv.Unquote("`Hello, world!`")
	fmt.Printf("%q, %v\n", str, err)

	str, err = strconv.Unquote("'\u263a'")
	fmt.Printf("%q, %v\n", str, err)

	str, err = strconv.Unquote("'\u2639\u2639'")
	fmt.Printf("%q, %v\n", str, err)
}

Output:

"", invalid syntax
"Hello, world!", <nil>
"Hello, world!", <nil>
"☺", <nil>
"", invalid syntax

Golang strconv Package »




Comments and Discussions!

Load comments ↻





Copyright © 2024 www.includehelp.com. All rights reserved.