mirror of
https://github.com/golang/go.git
synced 2025-05-05 23:53:05 +00:00
explain in details why to return struct type in interface constructor
parent
956b489bc7
commit
b61e691f20
@ -351,7 +351,8 @@ Go interfaces generally belong in the package that uses values of the
|
|||||||
interface type, not the package that implements those values. The
|
interface type, not the package that implements those values. The
|
||||||
implementing package should return concrete (usually pointer or struct)
|
implementing package should return concrete (usually pointer or struct)
|
||||||
types: that way, new methods can be added to implementations without
|
types: that way, new methods can be added to implementations without
|
||||||
requiring extensive refactoring.
|
requiring extensive refactoring, and this will enable the struct to implement
|
||||||
|
more than one interface and be accepted/evaluated to all of them in different functions
|
||||||
|
|
||||||
Do not define interfaces on the implementor side of an API "for mocking";
|
Do not define interfaces on the implementor side of an API "for mocking";
|
||||||
instead, design the API so that it can be tested using the public API of
|
instead, design the API so that it can be tested using the public API of
|
||||||
@ -400,6 +401,32 @@ func (t Thinger) Thing() bool { … }
|
|||||||
func NewThinger() Thinger { return Thinger{ … } }
|
func NewThinger() Thinger { return Thinger{ … } }
|
||||||
```
|
```
|
||||||
|
|
||||||
|
```go
|
||||||
|
// DONOT DO THIS
|
||||||
|
package main
|
||||||
|
import ("io")
|
||||||
|
type Thinger interface {
|
||||||
|
Thing() bool
|
||||||
|
}
|
||||||
|
type defaultThinger struct{ }
|
||||||
|
func (t defaultThinger) Thing() bool {
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
func (t defaultThinger) Read(p []byte) (n int, err error) {
|
||||||
|
return 0, nil
|
||||||
|
}
|
||||||
|
func NewThinger() Thinger {
|
||||||
|
return defaultThinger{ }
|
||||||
|
}
|
||||||
|
func main() {
|
||||||
|
testThinger(NewThinger())
|
||||||
|
// will return cannot use NewThinger() (type Thinger) as type io.Reader in argument to testReader: Thinger does not implement io.Reader (missing Read method)
|
||||||
|
// if NewThinger() return defaultThinger it will work
|
||||||
|
testReader(NewThinger())
|
||||||
|
}
|
||||||
|
func testReader(r io.Reader) {}
|
||||||
|
func testThinger(t Thinger) {}
|
||||||
|
```
|
||||||
|
|
||||||
## Line Length
|
## Line Length
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user