Skip to main content

Context

GORM 支持 Context,你可以使用 WithContext 方法将 Context 传递给 GORM。

// 单会话模式
db.WithContext(ctx).Find(&users)

Context 可以用于超时控制:

ctx, cancel := context.WithTimeout(context.Background(), 2*time.Second)
defer cancel()

db.WithContext(ctx).Find(&users)