dpzyd8865 2013-07-22 03:12
浏览 40
已采纳

golang:给定字符串,输出等效的golang字符串文字

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

This program when invoked should output the following:

"Hello World!"
a
"a"
"\"a\""
"This is a
test!"
"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条回答 默认 最新

  • dougeqiang1619 2013-07-22 03:47
    关注

    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.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 华为ensp模拟器中S5700交换机在配置过程中老是反复重启
  • ¥15 java写代码遇到问题,求帮助
  • ¥15 uniapp uview http 如何实现统一的请求异常信息提示?
  • ¥15 有了解d3和topogram.js库的吗?有偿请教
  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看
  • ¥15 关于#Java#的问题,如何解决?
  • ¥15 加热介质是液体,换热器壳侧导热系数和总的导热系数怎么算
  • ¥100 嵌入式系统基于PIC16F882和热敏电阻的数字温度计