在Go语言中编写全局函数(类似于append())

huangapple go评论105阅读模式
英文:

Writing Global Function in Go -- like append()

问题

问题很简单 - 如何在Go语言中实现一个与包无关的全局函数。

如果我在一个名为core的包中有一个函数,那么在另一个包中我需要使用core.Function()来调用该函数。但是在Go语言的实现中,有一些函数,比如make()、append()用于切片,可以在任何包中直接使用,而无需导入。如果我想要编写这样的函数,有什么方法可以实现吗?如果可能的话,我该如何编写这样的函数。

英文:

The Question is simple - how can i implement a package independent global function in golang.

ID, if i have a function in an package called core then from another package i need core.Function() to call that function. But i In go implementation there are some functions like - make(), append() for slice that can used without any import and within any package directly. If i wanted to write function like this what is the way of doing this? how can i write any function like this if it is possible.

答案1

得分: 12

这是你要翻译的内容:

无法完全实现你想要的,但可以通过使用点导入(dot-import)来接近。例如,如果你点导入(dot-import)fmt包,你可以将fmt.Println简写为Println

package main

import . "fmt"

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

Playground: http://play.golang.org/p/--dWV6PHYA.

英文:

It's not possible to do exactly what you want but you can get somewhat close to it by using a dot-import. E.g. if you dot-import the fmt package, you can spell fmt.Println as just Println:

package main

import . "fmt"

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

Playground: http://play.golang.org/p/--dWV6PHYA.

答案2

得分: 4

简短的回答是:你不能这样做。Go语言有内置的函数和类型,但即使是语言设计者也试图将其数量保持最少,并尽可能避免使用它们。例如,有一个内置的printf函数,但建议使用fmt.Printf代替,因为内置函数“不能保证会一直存在于语言中”。

虽然每次使用函数都需要在前面加上包名可能看起来很麻烦,但它有明显的优点(使代码更易读,避免名称冲突),实际上并没有听起来那么糟糕,如果包设计者遵循了这篇博文中描述的指南。

例如:要从模式字符串创建一个Regexp对象,你调用regexp.Compile(),而不是regexp.CompileRegexp() - 因为在调用函数时使用了包名,函数名可以缩短。当然,如果按照Ainar-G的建议使用“点导入”,那么你只需要使用Compile(),这可能会让人感到困惑(“编译什么?”)。

英文:

The short answer is: you can't. Go has builtin functions and types, but even the language designers try to keep their number to a minimum and avoid them if possible. For instance, there is a builtin printf function, but it is recommended to use fmt.Printf instead, as the builtin function "is not guaranteed to stay in the language".

While having to prepend the package name every time you use a function may appear cumbersome, it has its obvious advantages (makes code easier to read, avoids name collisions) and isn't actually as bad as it sounds if the package designer followed the guidelines described under "Naming package contents" in this blog post.

Example: to create a Regexp object from a pattern string, you call regexp.Compile(), not regexp.CompileRegexp() - since you use the package name when calling a function, the name of the function can be shortened. This is of course lost if you use the "dot import" as per Ainar-G's suggestion. Then you would have just Compile(), which might be confusing ("compile what?").

huangapple
  • 本文由 发表于 2015年10月12日 16:29:07
  • 转载请务必保留本文链接:https://go.coder-hub.com/33076502.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定