GO – package dependencies and initialization

I am new to Go, trying to find a solution to the following problems.

>The goal is to write all logs to a file.
>Package The main import package A.
>In package main, in the main method, the log record is set to write to the file.
>Package A has an init function. In A’s init function, there is a log line –log. Fatal(“I am package A”).
>Since the main package imports A, first call A’s init function (even before we have the opportunity to set up the log to write to the file).

How Solve this problem so that the log “I am package A” is written to the file?

Move the log record initialization to the log package imported by main and A, and do not directly or Indirectly depends on the part of the application you wish to log into the file during initialization. From the description of the init sequence in Effective Go and the spec, this should be enough to ensure that your logging is initialized first.

< p>I am new to Go, trying to find a solution to the following problems.

>The goal is to write all logs to a file.
>Package master import package A.
> In package main, in the main method, logging is set to write to a file
>Package A has an init function. In A’s init function, there is a log line –log.Fatal(“I am package A”) .
>Since the main package imports A, first call A’s init function (even before we have the opportunity to set up the log to write to the file).

How to solve this problem so that the log “I am “Package A” is written to the file?

Move the log record initialization to the log package imported by main and A, and do not directly or indirectly rely on the log file you want to log in during initialization. Application part. From the description of the init sequence in Effective Go and the spec, this should be enough to ensure that your logging is initialized first.

Leave a Comment

Your email address will not be published.