9

在 Go 中,是否存在以下 gettext 短格式的情况:

_("String to be translated.")

可以用吗?其中一个我相当确定答案是“不”的时候,但问以防万一我忽略了一些东西。我认为可以实现的最好的是:

import . "path/to/gettext-package"
...
s := gettext("String to be translated.")

由于下划线具有非常特定的含义,并且尝试定义名为 '_' 的函数会导致编译时错误“无法使用 _ 作为值”。

4

1 回答 1

15

No.空白标识符

...不引入新的绑定。

IOW,您可以声明命名的“事物” _,但不能使用该“名称”以任何方式引用它们。

但是,可以接近目标:

package main

import "fmt"

var p = fmt.Println

func main() {
        p("Hello, playground")
}

(也在这里

ie. you can bind any (local or imported) function to a variable and later invoke the function through that variable, getting rid of the package prefix - if you think that's handy. IMO not, BTW.

于 2013-01-12T12:07:43.110 回答