Your Question

the use code:

ctx := context.WithValue(context.Background(), "foo", "bar")
fmt.Println("----->after with value:", ctx.Value("foo"))
db.WithContext(ctx).First(...)

the init code:

db, err := gorm.Open(mysql.Open(conf.DSN(index)), &gorm.Config{
    Logger: l,
})
if err != nil {
    return nil, err
}
db.Callback().Query().Before("gorm:query").Register("my_plugin:before_query", func(d *gorm.DB) {
    // get the context here, we set by db.WithContext before
    // ctx := ...
    fmt.Println("----->my_plugin:before_query:", time.Now().Unix(), db.Statement.Context.Value("foo"), d.Statement.SQL.String())
})
db.Callback().Query().After("gorm:query").Register("my_plugin:after_query", func(d *gorm.DB) {
    fmt.Println("----->my_plugin:after_query:", time.Now().Unix(), db.Statement.Context.Value("foo"), d.Statement.SQL.String())
})

i try use db.Statement.Context, but it do not work, can't get the key "foo"

Comment From: ekristen

Was there a solution to this?

Comment From: ekristen

@xsddz did you ever find a solution?