Trusted answers to developer questions
Trusted Answers to Developer Questions

Related Tags

golang
strconv
communitycreator

How to use the strconv.Unquote() function in Golang

Maria Elijah

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 Golang


package main

import (
	"fmt"
	"strconv"
)
func main() {

  	// declare variable s, err and assign value to it
	s, err := strconv.Unquote("`Golang is one of my favourite programming languages`")
	// display result
	fmt.Printf("The value for s: %q, and error: %v\n", s, err)

	// reassigning value to s, err
	s, err = strconv.Unquote("Golang is one of my favourite programming languages")
	// display result
	fmt.Printf("The value for s: %q, and error: %v\n", s, err)
}

Explanation

  • Line 4: We add the main package.
  • 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 s and err, pass a value to the Unquote() function, and assign it to the variables.
  • Line 20: We print the results.

Note: If the value of s is syntactically valid, then the error will be <nil>.

RELATED TAGS

golang
strconv
communitycreator
RELATED COURSES

View all Courses

Keep Exploring