有人可以解释如何在下面的示例中正确使用Scan()
and吗?Value()
我正在尝试使用以下示例:
https://github.com/jinzhu/gorm/issues/2017#issuecomment-537627961
https://github.com/travisjeffery/proto-go-sql/blob/master/_example/person_sql.go
我的原型:
message Timestamp {
google.protobuf.Timestamp timestamp = 1;
}
message User {
uint32 ID = 1;
Timestamp createdAt = 2;
}
代码(需要修复time.Now()):
package v1
import (
"database/sql/driver"
"fmt"
"time"
"github.com/golang/protobuf/ptypes"
)
func (u *User) Create() {
u.CreatedAt = time.Now() // FIXME: ERROR. How do I make use of Scan() and Value() here?
// saving to SQL database
}
func (ts *Timestamp) Scan(value interface{}) error {
switch t := value.(type) {
case time.Time:
var err error
ts.Timestamp, err = ptypes.TimestampProto(t)
if err != nil {
return err
}
default:
return fmt.Errorf("Not a protobuf Timestamp")
}
return nil
}
func (ts Timestamp) Value() (driver.Value, error) {
return ptypes.Timestamp(ts.Timestamp)
}
智慧大石
相关分类