strconv.UnquoteChar()
functionThe strconv package’s UnquoteChar()
function decodes the first character or byte in an escaped string or character literal represented by the string s
.
func UnquoteChar(s string, quote byte) (value rune, multibyte bool, tail string, err error)
s
: This represents the quoted string.quote
: This is the byte value that has to be checked.The UnquoteChar()
function returns four values:
value
: This is a decoded Unicode point value of the rune type (i.e., the quote character).multibyte
: This is a Boolean value that specifies whether a multibyte UTF-8
representation is required for the decoded character.tail
: This is a string that follows the initial quote character and is the last string.error
: This is returned when the character is not syntactically valid. If the character is syntactically valid, then the error will be <nil>
.The following code shows how to use the UnquoteChar()
function in Golang.
// using strconv.UnquoteChar() Function in Golangpackage mainimport ("fmt""strconv")func main() {// declare variable s and assign value to itvar s = "`Golang is one of my favourite programming languages`"// declare variable value, mb, tail, err and assign value to it// represent the return valuesvalue, mb, tail, err := strconv.UnquoteChar(s, '`')// Checks if the character is syntactically validif err != nil {fmt.Println("Error:", err)}// display resultfmt.Println("value:", string(value))fmt.Println("multibyte:", mb)fmt.Println("tail:", tail)fmt.Println()// reassigning values = `\"Educative shot on UnqouteChar() function\"`// display resultfmt.Println(strconv.UnquoteChar(s, '"'))}
main
package.main()
function.s
of the string type and assign a value to it.value
, mb
, tail
, and err
. These variables represent the return values. Next, we pass the variable s
in the UnquoteChar()
function and assign it to variables value
, mb
, tail
, and err
.if
statement to check if the character is syntactically valid. If the condition is satisfied, the error will be displayed.value
.mb
.tail
.s
.s
in the UnquoteChar()
function and display the result.