Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8500219
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T00:53:11+00:00 2026-06-11T00:53:11+00:00

The golang package net/http uses the global variable DefaultServeMux to register handlers. Is this

  • 0

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 variable after all?

The two main reasons not to use global variables are AFAIK 1) that they add to complexity and 2) are problematic in concurrent programs.

Maybe 1) is not considered important in this case because the developer can choose not to use DefaultServerMux? What about 2)? Are global variables always thread/goroutine safe in Go? Still, I’m surprised that it’s used in Go’s standard library. I’ve never seen such practice in other languages / standard libraries.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-11T00:53:13+00:00Added an answer on June 11, 2026 at 12:53 am

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

If you look at the image package here http://golang.org/src/pkg/image/image.go you can see that the
I'm trying to set cookies with Go's net/http package. I have: package main import
I am running this code in the sandbox in http://tour.golang.org/ I thought that once
I read in this presentation http://golang.org/doc/ExpressivenessOfGo.pdf page 42: Safe - no stack overflows How
I tried to use this tutorial with Golang: http://www.opengl-tutorial.org/beginners-tutorials/tutorial-2-the-first-triangle/ The go-version opens the window
I try to call the ExpFloat64() function of the rand package (http://golang.org/pkg/rand/). However, it
In one of the example servers given at golang.org: package main import ( flag
I follow the installation guide at http://golang.org/doc/install.html , at first everything goes well, but
I noticed that garbage collection is not yet implemented in gccgo. http://golang.org/doc/gccgo_install.html#Unimplemented Does the
Having read the following at http://golang.org/doc/effective_go.html#arrays ... Arrays are values. Assigning one array to

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.