當前位置: 首頁>>編程示例 >>用法及示例精選 >>正文


GO Unquote用法及代碼示例

GO語言"strconv"包中"Unquote"函數的用法及代碼示例。

用法:

func Unquote(s string)(string, error)

Unquote 將 s 解釋為單引號、雙引號或反引號 Go 字符串文字,返回 s 引用的字符串值。 (如果 s 是單引號,它將是 Go 字符文字;取消引用返回相應的 one-character 字符串。)

例子:

package main

import (
	"fmt"
	"strconv"
)

func main() {
	s, err := strconv.Unquote("You can't unquote a string without quotes")
	fmt.Printf("%q, %v\n", s, err)
	s, err = strconv.Unquote("\"The string must be either double-quoted\"")
	fmt.Printf("%q, %v\n", s, err)
	s, err = strconv.Unquote("`or backquoted.`")
	fmt.Printf("%q, %v\n", s, err)
	s, err = strconv.Unquote("'\u263a'") // single character only allowed in single quotes
	fmt.Printf("%q, %v\n", s, err)
	s, err = strconv.Unquote("'\u2639\u2639'")
	fmt.Printf("%q, %v\n", s, err)

}

輸出:

"", invalid syntax
"The string must be either double-quoted", <nil>
"or backquoted.", <nil>
"☺", <nil>
"", invalid syntax

相關用法


注:本文由純淨天空篩選整理自golang.google.cn大神的英文原創作品 Unquote。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。