GoLang多租户应用程序数据库连接

I am new to golang and currently trying to build a multi-tenant application. In my application each tenants have their own database. My requirement is that I need to switch database connection per tenant request, how can this be accomplished in golang. I prefer postgresql for this project.

I can have a map of database connection to tenant, but not sure if this is a good practice.

Your help and suggestion is highly appreciated

I've handled a similar kind of requirement in Rails. Probably you can use the same approach in go-lang also.

I will have one master DB, which will hold just the tenant information. Like tenant name and db_name.

And I've got a rack middleware which will switch DB based on a subdomain(I'm using the subdomain to identify the tenant).

For example, Your master DB can have table tenants and an example record might probably look like this: { id: 1, name: 'XYZ', db_name: 'xyz' }

and when your application receives a request with subdomain xyz, Your middleware should switch to xyz DB.