Can I transfer the collection as parameter of func in golang


I connect the database then get the client, then I use the client to get the collection in the db, i.e. a collection called meetingCol. Can I save this meetingCol in an object, i.e. meetingService, then in the func of meetingService, like GetMeeting(), I want to use the meetingCol to retrieve contents in this collection. The code looks like:

meetingService {
  collection : meetingCol
func (meetingService meetingService) GetMeeting(ctx context.Context, id int64) {
  collection = meetingService.collection //here I will use the collection
  //.. to find content by id in the collection

The code can work but I am not sure if the GetMeeting is thread safe in this way, if there are hundreds of client call GetMeeting, will that be OK for this kind of using and accessing the collection? do I need to transfer the mongo.Client as parameter to the meetingService, or to transfer the mongo.Database to the meetingService?


It is safe to use a mongo.Collection instance concurrently. The mongo.Collection type is immutable, so you can have multiple clients call GetMeeting concurrently without any issues.

Hello Divjot, many thanks for your answer.