dq23171 2013-01-12 12:05
浏览 29
已采纳

有什么办法可以将函数名别名为“ _”?

In Go, is there any circumstance where the gettext short-form of:

_("String to be translated.")

can be used? One of those times where I'm fairly certain the answer is 'no', but asking just in case I've overlooked something. I'm thinking the best that can be achieved is:

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

since underscore has a very specific meaning, and attempting to define a function named '_' results in the compile-time error "cannot use _ as value".

  • 写回答

1条回答 默认 最新

  • douyun1860 2013-01-12 12:07
    关注

    No. The blank identifier

    ... does not introduce a new binding.

    IOW, you can declare "things" named _ but you cannot refer to them in any way using that "name".

    However, one can get close to the goal:

    package main
    
    import "fmt"
    
    var p = fmt.Println
    
    func main() {
            p("Hello, playground")
    }
    

    (also here)

    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.

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

报告相同问题?

悬赏问题

  • ¥50 易语言把MYSQL数据库中的数据添加至组合框
  • ¥20 求数据集和代码#有偿答复
  • ¥15 关于下拉菜单选项关联的问题
  • ¥20 java-OJ-健康体检
  • ¥15 rs485的上拉下拉,不会对a-b<-200mv有影响吗,就是接受时,对判断逻辑0有影响吗
  • ¥15 使用phpstudy在云服务器上搭建个人网站
  • ¥15 应该如何判断含间隙的曲柄摇杆机构,轴与轴承是否发生了碰撞?
  • ¥15 vue3+express部署到nginx
  • ¥20 搭建pt1000三线制高精度测温电路
  • ¥15 使用Jdk8自带的算法,和Jdk11自带的加密结果会一样吗,不一样的话有什么解决方案,Jdk不能升级的情况