Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Any way to alias a function name to '_'?

Tags:

go

gettext

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".

like image 458
Rich Churcher Avatar asked Jan 12 '13 12:01

Rich Churcher


1 Answers

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.

like image 150
zzzz Avatar answered Oct 14 '22 11:10

zzzz