golang:给定一个字符串,输出等价的golang字符串 [英] golang: given a string, output an equivalent golang string literal

查看:121
本文介绍了golang:给定一个字符串,输出等价的golang字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

编写go应用程序输出有效的go代码可能是使用内置的go包和其子包(go / ast,go / token,go / printer,创建一个字符串表达式,你需要创建一个ast.BasicLit:

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

在我的go程序中,我有一个字符串,我需要创建一个ast.BasicLit,当输出将生成一个字符串文字时忠实地再现相同的串。为了做到这一点,我必须从字符串中派生出一个表示字符串的go-syntax字面值的字符串。 (这个概念是如此元数据,这是很难描述没有歧义。)

我在寻找的东西基本上相当于Python内置的repr( )。这是一种操作,您可以称它与JavaScript中eval()的相反。



一个示例应该有助于说明我在寻找什么。

 包裹主要

导入(
repr


//假设我希望找到的操作在包repr中作为名为func(v string)string的名为StrLit()的函数实现。

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 \\\
test!))
println(repr.StrLit(As is\x00this!))
}

这个程序在被调用时应该输出如下内容:

 Hello World! 
a
a
\a\
这是一个测试!
正如x00this!

虽然我的具体问题是关于字符串,但我会对可以处理值的通用解决方案感兴趣如下所示:

  package main 
$ b $(任何类型,浮点类型,甚至复杂类型) b import(
repr


//假设这次repr.StrLit()具有签名func(v interface {})string。
$ b $ 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

我通过标准的库文档查看了一下,但并没有真正发现任何看起来接近我所寻找的东西。希望你们能帮助我。



提前致谢!

>您需要使用%#v格式化程序 http://golang.org/pkg/fmt/#Sprintf



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



请参阅: http://play.golang.org/p/nFAKFObXE5 为一个例子,包括各种不同的类型,包括像结构这样的复杂文字。



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


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!

解决方案

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.

这篇关于golang:给定一个字符串,输出等价的golang字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆