func GetByID(id int64) (*User, error) { var u User err := db(). Table("users"). Where("id = ?", id). First(&u). Error if err != nil { if gorm.IsRecordNotFoundError(err) { return nil, ErrNotFound } return nil, err } return &u, nil }
func (r *Repository) List(ctx context.Context, filters Filters) ([]User, error) { db := r.db if filters.PartyID != nil { db = db.Where("party_id = ?", *filters.PartyID) } var ( us []User err error ) err = errors.WithStack(db.WithContext(ctx). Find(&us). Error) if err != nil { return nil, err } return us, nil }In this example, the "List" function uses the "ctx context.Context" to pass context to the "WithContext" function. The "WithContext" function returns a new database query object that uses the provided context. This allows the function to propagate the context provided to it to any other functions that require the context. In conclusion, "github.com/microcosm-cc/microcosm/models" is a package library that uses Go's built-in Context package to propagate request-scoped values across API boundaries and between processes. The examples provided above demonstrate how this package library uses the Context package to pass context between functions.