Skip to content
Permalink
Branch: master
Find file Copy path
Find file Copy path
Fetching contributors…
Cannot retrieve contributors at this time
61 lines (47 sloc) 1.28 KB
// All material is licensed under the Apache License Version 2.0, January 2004
// http://www.apache.org/licenses/LICENSE-2.0
// Sample program to show how polymorphic behavior with interfaces.
package main
import "fmt"
// reader is an interface that defines the act of reading data.
type reader interface {
read(b []byte) (int, error)
}
// file defines a system file.
type file struct {
name string
}
// read implements the reader interface for a file.
func (file) read(b []byte) (int, error) {
s := "<rss><channel><title>Going Go Programming</title></channel></rss>"
copy(b, s)
return len(s), nil
}
// pipe defines a named pipe network connection.
type pipe struct {
name string
}
// read implements the reader interface for a network connection.
func (pipe) read(b []byte) (int, error) {
s := `{name: "bill", title: "developer"}`
copy(b, s)
return len(s), nil
}
func main() {
// Create two values one of type file and one of type pipe.
f := file{"data.json"}
p := pipe{"cfg_service"}
// Call the retrieve function for each concrete type.
retrieve(f)
retrieve(p)
}
// retrieve can read any device and process the data.
func retrieve(r reader) error {
data := make([]byte, 100)
len, err := r.read(data)
if err != nil {
return err
}
fmt.Println(string(data[:len]))
return nil
}
You can’t perform that action at this time.