golang:给定一个字符串,输出一个等效的golang字符串字面量。

huangapple go评论81阅读模式
英文:

golang: given a string, output an equivalent golang string literal

问题

使用内置的"go"包及其子包("go/ast"、"go/token"、"go/printer"等)编写输出有效go代码的go应用程序可能是最好的方法。

要创建一个字符串文字表达式,您需要创建一个ast.BasicLit:

l := &ast.BasicLit{Kind: token.STRING, Value: "\"Hello world!\""}

在我的go程序中,我有一个字符串,我需要创建一个ast.BasicLit,当输出时,它将产生一个字符串文字,忠实地复制相同的字符串。为了做到这一点,我必须从字符串中派生出一个表示go语法文字的字符串。(这个概念非常元,很难没有歧义地描述。)

在go中,我要找的基本上是Python内置的repr()的等效物。它是一个你可以称之为JavaScript中eval()的“相反”操作。

一个例子应该有助于说明我要找的东西。

package main

import (
    "repr"
)

// 假设我希望在包“repr”中实现这个操作,作为一个名为“StrLit()”的函数,其签名为“func(v string) string”。

func main() {
    println(repr.StrLit("Hello World!"))
    println("a")
    println(repr.StrLit("a"))
    println(repr.StrLit(repr.StrLit("a")))
    println(repr.StrLit("This is a\ntest!"))
    println(repr.StrLit("As is\x00this!"))
}

当调用此程序时,应输出以下内容:

"Hello World!"
a
"a"
"\"a\""
"This is a\ntest!"
"As is\x00this!"

虽然我具体的问题涉及字符串,但我对一个通用解决方案也很感兴趣,它可以适用于任何类型的值(整数类型、浮点类型,甚至复数类型),如下所示:

package main

import (
    "repr"
)

// 假设这次repr.StrLit()的签名为“func(v interface{}) string”。

func main() {
    var a int = 5
    println(repr.StrLit(a))
    var c complex128 = 1.0+1.0i
    println(repr.StrLit(c))
}

此程序应输出:

5
1.0+1.0i

我已经在标准库文档中查找了很多,但实际上没有找到任何看起来接近我要找的东西。希望你们能帮助我。

提前感谢!

英文:

Writing go applications which output valid go code is probably best done using the built-in "go" package and some of its sub-packages ("go/ast", "go/token", "go/printer", etc).

To create a string-literal expression, you need to create an ast.BasicLit:

l := &ast.BasicLit{Kind: token.STRING, Value: "\"Hello world!\""}

In my go program, I've got a string and I need to create an ast.BasicLit which when output will produce a string literal which faithfully reproduces the same string. In order to do that, I must derive from the string a string which represents the go-syntax literal which represents the string. (This concept is so meta, it's difficult to describe without ambiguity.)

What I'm looking for in go is basically the equivalent of the Python built-in repr(). It's an operation which you might call the "opposite" of what eval() in JavaScript does.

An example should help illustrate what I'm looking for.

package main

import (
    "repr"
)

// Assume the operation I'm hoping to find is implemented in the package "repr" as a function called "StrLit()" with the signature "func(v string) string".

func main() {
    println(repr.StrLit("Hello World!"))
    println("a")
    println(repr.StrLit("a"))
    println(repr.StrLit(repr.StrLit("a")))
    println(repr.StrLit("This is a\ntest!"))
    println(repr.StrLit("As is\x00this!"))
}

This program when invoked should output the following:

"Hello World!"
a
"a"
"\"a\""
"This is a\ntest!"
"As is\x00this!"

While my specific issue regards strings, I'd be interested in a general solution which would work on values of any type (integer types, float types, even complex types) as follows:

package main

import (
    "repr"
)

// Assume this time that repr.StrLit() has the signature "func(v interface{}) string".

func main() {
    var a int = 5
    println(repr.StrLit(a))
    var c complex128 = 1.0+1.0i
    println(repr.StrLit(c))
}

This program should output:

5
1.0+1.0i

I've looked quite a bit through the standard library documentation but haven't really found anything which looks close to what I'm looking for. Hopefully you folks can help me.

Thanks in advance!

答案1

得分: 14

你想要使用http://golang.org/pkg/fmt/#Sprintf和%#v格式化符号。

lit := fmt.Sprintf("%#v", "foo")将会打印出"foo"。

参考:http://play.golang.org/p/nFAKFObXE5,其中包含了各种不同类型的示例,包括结构体等复杂字面量。

fmt包有很多有用的格式化动词,所以一定要查看它的文档。

英文:

You want http://golang.org/pkg/fmt/#Sprintf with the %#v formatter.

lit := fmt.Sprintf("%#v", "foo") will print out "foo"

See: http://play.golang.org/p/nFAKFObXE5 for an example with various different types including complex literals like structs.

The fmt package has a lot of useful format verbs so be sure to check out the rundown in it's docs.

答案2

得分: 0

这个代码会完全按照你的要求执行:

package main
import "strconv"

func main() {
println(strconv.Quote("Hello World!"))
println("a")
println(strconv.Quote("a"))
println(strconv.Quote(strconv.Quote("a")))
println(strconv.Quote("This is a\ntest!"))
println(strconv.Quote("As is\x00this!"))
}


https://golang.org/pkg/strconv#Quote

<details>
<summary>英文:</summary>

This does exactly what you want:

package main
import "strconv"

func main() {
println(strconv.Quote("Hello World!"))
println("a")
println(strconv.Quote("a"))
println(strconv.Quote(strconv.Quote("a")))
println(strconv.Quote("This is a\ntest!"))
println(strconv.Quote("As is\x00this!"))
}


https://golang.org/pkg/strconv#Quote

</details>



huangapple
  • 本文由 发表于 2013年7月22日 11:12:30
  • 转载请务必保留本文链接:https://go.coder-hub.com/17779371.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定