如何在Go-lang中的包之间共享变量

I have written a simple go application and formatted it in to several packages.

+main
+controllers
+handlers
+commons
+utils

I'm starting the server in main package and reading server specific parameters at the startup.

I won't to use those parameters, such as DB properties from commons package. I cannot import main package in to commons 'cause commons is a dependency to controllers, handlers which is a dependency to main. So this causes cyclic import.

How should we handle this kind of situation?. Is it possible to read the configuration once and refer to it inside commons package whenever needed? Example would be helpful.

You can use a config package and when you need to use a config variable then you can send to another struct into different package.

main() {
    config := config{}
    user = NewUser(config)
    ...
}

I recommend to you check the config revel package (https://github.com/revel/config). It's used into revel framework https://github.com/revel/revel.