Home > Article > Backend Development > What this does: *f = Foo(value)
php editor Zimo is here to introduce a common code fragment: f = Foo(value). What this code does is creates a Foo object named f and passes value as a parameter to its constructor. In this way, we can use the Foo object in the program and operate and process it. A Foo object may be an instance of a class, which may have various properties and methods used to implement specific functionality. By creating an object and passing parameters, we can dynamically initialize the object's properties as needed to meet the program's requirements. This is a commonly used code snippet that can be used in many different programming scenarios.
I found the implementation of the database/sql.Scanner
interface, but I don’t quite understand it?
can be compiled
The part I get is Foo
is an integer and it has method Scan
Scan
method returns error
, but how to return fmt.Errorf("Invalid database type: %T %v", value, value)
? Isn't this a bug or is it?
*f = Foo(value)
What does it do? When you pass this type to rows.Scan()
, does it call Foo.Scan()
? If the type is expected int64
it will call itself Foo(value)
?
type Foo int // Scan implements the database/sql.Scanner interface func (f *Foo) Scan(value interface{}) error { switch value := value.(type) { case int64: *f = Foo(value) default: return fmt.Errorf("Invalid database type: %T %v", value, value) } return nil }
fmt.Errorf(format, args)
Returns the error value created using errors.New(str)
, where str
is the format String (your error message).
Line *f = Foo(value)
Converts the int64
value to the Foo
type and assigns it to the method receiver (note f
is a pointer).
The above is the detailed content of What this does: *f = Foo(value). For more information, please follow other related articles on the PHP Chinese website!