Go是一种具有许多
functional programming元素的
procedural programming语言.
函数是Go中的第一类类型,您可以创建函数类型的变量,并且可以为这些变量分配函数值,您可以调用它们(存储在它们中的函数值).
所以你可以这样做:
var myprint = fmt.Println
myprint("Hello, playground")
myprint(1, 3.14, "\n", time.Now())
Hello, playground
1 3.14
2009-11-10 23:00:00 +0000 UTC m=+0.000000001
上述myprint变量的优点是它具有与fmt.Println()相同的签名,即:
func Println(a ...interface{}) (n int, err error)
是的,我们还可以创建一个包装函数,如:
func myprint(a ...interface{}) (n int, err error) {
return fmt.Println(a...)
}
更多关于这个主题:
另一种可能性是使用“点导入”,但我建议反对它:
import (
. "fmt"
)
func main() {
Println("Hello, playground")
}
If an explicit period (.) appears instead of a name, all the package’s exported identifiers declared in that package’s 07009 will be declared in the importing source file’s file block and must be accessed without a qualifier.