Is “net/http”'s use of global variables considered a good practice in golang?

前端 未结 2 524
星月不相逢
星月不相逢 2021-02-07 09:30

The golang package \"net/http\" uses the global variable DefaultServeMux to register handlers. Is this considered a good practice or even an golang idiom? Is it a global variabl

2条回答
  •  醉梦人生
    2021-02-07 10:11

    Is it a global variable after all?

    Yes. The variable is defined on root level, which makes it global throughout the package.

    However, this is not a global variable which stores all the sensible information of the net/http package. It is merely a convenience setup which uses the content of the net/http package to provide an quickstart opportunity to the user. This also means, that is does not add much complexity.

    Is this considered a good practice or even an golang idiom?

    IMO, it is good practice to aid the user with the usage of a package. If you're finding that you could save the user some time by providing a good default configuration, do so.

    However, you should be careful when you're about to export variables. They should be made ready for concurrent access. The DefaultServeMux (or better, the underlying ServeMux), for example, is using a mutex to be thread safe.

    Are global variables always thread/goroutine safe in Go?

    No. Without proper synchronization (mutex, channel, ...), everything that is accessed concurrently is problematic and will most certainly blow everything to bits and pieces.

    I've never seen such practice in other languages / standard libraries.

    Python's logging module, for example, provides a function to retrieve the root logging object, which one can call methods on to customize the logging behaviour. This could be seen as a global object, as it is mutable and defined in the module.

提交回复
热议问题