drn5375 2018-11-27 05:22
浏览 25

如何在golang的额外软件包中使用主软件包记录器?

I want to make global logger.

Can I make global logger? (To access every pacakges)

  • 写回答

1条回答 默认 最新

  • drk7700 2018-11-27 07:10
    关注

    Packages can have global variables:

    package somepackage
    
    import "log"
    
    var (
      Log *log.Logger = log.New(os.Stderr, "", log.LstdFlags)
    )
    

    Notice that I capitalized Log. This means it is exported (think public in other languages).

    In general though, using globals is discouraged. I would recommend thinking of a different pattern.

    评论

报告相同问题?