A statement interfaces
1 type Result interface { 2 LastInsertId() (int64, error) 3 RowsAffected() (int64, error) 4 }
Second, implement the interface, the interface puts here as a member variable, and then will achieve the conversion interface is call interface, only the interface package, in fact, did not really realize, but wait for others to achieve
1 // a lock 2 // false set of interfaces to achieve a result, a functional need, let others be embodied. Pretend implements an interface method actually calls the corresponding internal interface 3 of the type driverResult struct { 4 sync.Locker 5 Resi driver.Result 6 } 7 // the Result is a summary of the executed SQL commands. . 8 // lastInsertId () returns an integer generated by the database, this integer is the response to the command. In inserting a new row of data, generally derived from the self-energizing integer data column in the data table. 9 // Not all databases support this feature, and individual statements database used in implementing this feature will be different. 10 // RowsAffected () returns subject to update, insert, or delete operation affects the number of rows, not all databases or all database drivers support this feature. . 11 type the Result interface { 12 is LastInsertId() (int64, error) 13 RowsAffected() (int64, error) 14 } 15 16 func (dr driverResult) LastInsertId() (int64, error) { 17 dr.Lock() 18 defer dr.Unlock() 19 return dr.resi.LastInsertId() 20 } 21 22 func (dr driverResult) RowsAffected() (int64, error) { 23 dr.Lock() 24 defer dr.Unlock() 25 return dr.resi.RowsAffected() 26 }