Home  >  Article  >  Backend Development  >  What does "package-level export" mean?

What does "package-level export" mean?

PHPz
PHPzforward
2024-02-05 22:15:081089browse

What does package-level export mean?

Question content

I am working on this logger. What exactly does this import mean?

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")
}

As you can see from the example, the "built-in" log has been replaced by the above implementation. I can't find any official/official specifications, maybe I don't know how this function is called.


Correct Answer


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

..Hmm..The example in the example is the withfields() function.

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

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

    log.WithFields(/* etc */)

In 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 the logrus.entry reference (returned by the withfields() function).

What's the big deal?

Package-level exports are common in go; a package that doesn't export anything isn't of much use to anyone! :)

I'm not sure if they are explicitly referenced as "package-level" exports; usually they are just "exports" or "export symbols" etc.

They are called specifically in the case of logrus because package authors use it to create a package that is "compatible" with the standard log package in golang (also using "package-level "Export). p>

That is, if you alias logrus to log, logrus provides a superset of the standard log package; this is intentional , so that existing code using the log package can be converted to logrus very quickly by simply changing the import statement in the file making the log call.

The above is the detailed content of What does "package-level export" mean?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:stackoverflow.com. If there is any infringement, please contact admin@php.cn delete