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 main

import (
    "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")
}
时间: 2024-08-10 00:04:28

A Tour of Go Interfaces are satisfied implicitly的相关文章

A Tour of Go Interfaces

An interface type is defined by a set of methods. A value of interface type can hold any value that implements those methods. Note: The code on the left fails to compile. Vertex doesn't satisfy Abser because the Abs method is defined only on*Vertex,

Go Object Oriented Design

Go hastypes and valuesrather than classes and objects. So can a language without classes or objects be object-oriented? While Go may not fit the typical mold of an OOP language, it does  provide many of the same features, albeit in a slightly differe

F - Free DIY Tour(动态规划)

这道题也可以用深搜做,可以深搜本来就不熟,好久没做早忘了,明天看看咋做的 Description Weiwei is a software engineer of ShiningSoft. He has just excellently fulfilled a software project with his fellow workers. His boss is so satisfied with their job that he decide to provide them a free

A Swifr Tour

Tradition suggests that the first program in a new language should print the words "Hello ,world!" on the screen. In Swift , this can be done in a single line :  print("Hello world") If you gave written code in C otr Objective - C , th

Go Methods and Interfaces

[Go Methods and Interfaces] 1.Go does not have classes. However, you can define methods on struct types. The method receiver appears in its own argument list between the func keyword and the method name. 2.You can declare a method on any type that is

HDU1224 Free DIY Tour 【SPFA】

Free DIY Tour Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 3939    Accepted Submission(s): 1262 Problem Description Weiwei is a software engineer of ShiningSoft. He has just excellently fulf

HDU 1224 Free DIY Tour(DP)

Problem Description Weiwei is a software engineer of ShiningSoft. He has just excellently fulfilled a software project with his fellow workers. His boss is so satisfied with their job that he decide to provide them a free tour around the world. It's

HDU 1224 Free DIY Tour 简单DP

Free DIY Tour Problem Description Weiwei is a software engineer of ShiningSoft. He has just excellently fulfilled a software project with his fellow workers. His boss is so satisfied with their job that he decide to provide them a free tour around th

18 A GIF decoder: an exercise in Go interfaces

A GIF decoder: an exercise in Go interfaces 25 May 2011 Introduction At the Google I/O conference in San Francisco on May 10, 2011, we announced that the Go language is now available on Google App Engine. Go is the first language to be made available