“package-level exported” 的意思是“包级别导出”。

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

What means "package-level exported"

问题

我正在翻译以下内容:

我正在研究这个日志记录器。这个导入语句具体是什么意思?

使用Logrus的最简单方法就是使用包级别的导出日志记录器:

package main

import (
  log "github.com/sirupsen/logrus"
)

func main() {
  log.WithFields(log.Fields{
    "animal": "walrus",
  }).Info("A walrus appears")
}

从示例中看起来,"内置"的日志记录器被上述实现所替代。我找不到任何官方/正式的规范,可能是因为我不知道这个功能被称为什么。

英文:

I was looking into this logger. What this import exactly means?

> The simplest way to use Logrus is simply the package-level exported logger:

package main

import (
  log "github.com/sirupsen/logrus"
)

func main() {
  log.WithFields(log.Fields{
    "animal": "walrus",
  }).Info("A walrus appears")
}

From the example it looks like that "built-in" log was replaces by the the above implementation. I couldn't locate any official/formal specs, probably I don't know how this feature is called.

答案1

得分: 0

这只是一个简单的参考,说明在包级别直接导出了函数。也就是说,不需要任何类型或变量引用就可以访问这些导出的符号。

示例中的示例是WithFields()函数。

这个函数是在“包级别”导出的,所以你只需要引用该函数的包名(或别名)。

在这种情况下,logrus包被别名为log,所以可以简单地使用以下方式调用它导出的WithFields()函数:

    log.WithFields(/* etc */)

相比之下,示例中的Info()函数演示了logrus包中另一个类型导出的符号(在这种情况下是一个函数);这里的Info()函数是在logrus.Entry引用上调用的(由WithFields()函数返回)。

这有什么大不了的?

在Go中,包级别的导出非常常见;一个没有导出任何内容的包对任何人来说都没有什么用处! “package-level exported” 的意思是“包级别导出”。

我不确定它们在一般情况下是否被明确称为“包级别”的导出;通常它们只会被称为“导出”或“导出的符号”等。

logrus的情况下,它们被特别提到,因为包的作者使用这种方式创建了一个与Go语言中的标准log包“兼容”的包(该包也使用“包级别”的导出)。

也就是说,如果你将logrus别名为log,那么logrus提供了标准log包的“超集”;这是有意为之的,这样就可以通过简单地更改调用log的文件中的导入语句,将使用log包的现有代码快速转换为logrus

英文:

This is simply a reference to the fact that there are functions exported directly at the package level. That is, they do not require any type or variable reference to access those exported symbols.

The example in the.. um.. example, is the WithFields() function.

This is exported at that "package level", so all you need in order to reference that function is the package name (or alias).

In this case, the logrus package has been aliased as log, so the WithFields() function it exports can be called simply using:

    log.WithFields(/* etc */)

By contrast, the Info() function in the example illustrates a symbol (in this case a function) exported by another type in the logrus package; the Info() function here is called on a logrus.Entry reference (returned by the WithFields() function).

What's the Big Deal?

Package-Level exports are very common in go; a package that didn't export anything wouldn't be very useful to anybody! “package-level exported” 的意思是“包级别导出”。

I'm not sure that they are explicitly referenced as "package-level" exports in general; usually they would just be "exports" or "exported symbols" etc.

They are called out specifically in the case of logrus because the package authors have used this to create a package that is 'compatible' with the standard log package in GoLang (which also uses "package-level" exports).

i.e. if you alias logrus as log then logrus provides a superset of the standard log package; this is deliberately so that existing code that uses the log package can be converted to logrus very quickly by simply changing the import statement in files that make log calls.

huangapple
  • 本文由 发表于 2022年12月6日 15:24:48
  • 转载请务必保留本文链接:https://go.coder-hub.com/74698723.html
匿名

发表评论

匿名网友

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

确定