Home  >  Article  >  Backend Development  >  How to connect to a non-default firestore database using Go?

How to connect to a non-default firestore database using Go?

王林
王林forward
2024-02-08 21:05:231153browse

如何使用 Go 连接到非默认 firestore 数据库?

In this article, php editor Xiaoxin will introduce to you how to use Go language to connect to a non-default Firestore database. Firestore is a flexible and scalable NoSQL document database, but by default it only connects to the default database for Google Cloud projects. However, sometimes we may need to connect to a different database, and this article will teach you how to achieve this. Whether you are a newbie or an experienced developer, this article will provide you with easy-to-follow steps and sample code to help you successfully connect to a non-default Firestore database. let's start!

Question content

I saw a post about using node to connect to a non-default Firestore database. I was wondering if anyone knows the same functionality in the firestore Go package?

I tried direct translation but no success. Maybe the package hasn't been updated with this feature yet?


Correct answer


Call NewClientWithDatabase to connect to a specific database.

ctx := context.Background()
client, err := firestore.NewClientWithDatabase(ctx, projectID, databaseID)
if err != nil {
    // TODO: Handle error.
}
defer client.Close() // Close client when done.

The above is the detailed content of How to connect to a non-default firestore database using Go?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:stackoverflow.com. If there is any infringement, please contact admin@php.cn delete