首页 > 代码库 > A Tour of Go Interfaces are satisfied implicitly
A Tour of Go Interfaces are satisfied implicitly
A type implements an interface by implementing the methods.
There is no explicit declaration of intent.
Implicit interfaces decouple implementation packages from the packages that define the interfaces: neither depends on the other.
It also encourages the definition of precise interfaces, because you don‘t have to find every implementation and tag it with the new interface name.
Package io defines Reader
and Writer
; you don‘t have to.
package mainimport ( "fmt" "os")type Reader interface { Read(b []byte) (n int, err error)}type Writer interface { Write(b []byte) (n int,err error)}type ReadWriter interface { Reader Writer}func main() { var w Writer //os.Stdout implements Writer w = os.Stdout fmt.Fprintf(w, "hello, writer\n")}
A Tour of Go Interfaces are satisfied implicitly
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。