【问题标题】:Convert Go rune to string将 Go 符文转换为字符串
【发布时间】:2019-04-29 22:52:42
【问题描述】:

我正在尝试将s := '{"selector:"{"Status":"open"}"}' 转换为类型string,因为我需要将其作为参数传递给使用GetQueryResult() 的查询。

我试过UnescapeString,它只接受字符串作为参数:

fmt.Println("args " ,html.UnescapeString(s)

但是 s 是一个 Go rune

【问题讨论】:

    标签: string go rune


    【解决方案1】:

    The Go Programming Language Specification

    String literals

    Rune literals


    使用 string 原始文字反引号,而不是 rune 文字单引号。


    例如,

    package main
    
    import (
        "fmt"
    )
    
    func main() {
        s := `{"selector:"{"Status":"open"}"}`
        fmt.Printf("type %T: %s", s, s)
    }
    

    游乐场:https://play.golang.org/p/lGARb35VHTv

    输出:

    type string: {"selector:"{"Status":"open"}"}
    

    【讨论】:

      猜你喜欢
      • 2012-08-02
      • 2013-02-21
      • 2021-12-07
      • 2021-01-06
      • 1970-01-01
      • 1970-01-01
      • 2017-05-20
      • 2019-10-10
      • 1970-01-01
      相关资源
      最近更新 更多