github.com/bgentry/go@v0.0.0-20150121062915-6cf5a733d54d/doc/progs/interface.go (about)

     1  // compile
     2  
     3  // Copyright 2012 The Go Authors. All rights reserved.
     4  // Use of this source code is governed by a BSD-style
     5  // license that can be found in the LICENSE file.
     6  
     7  // This file contains the code snippets included in "The Laws of Reflection."
     8  
     9  package main
    10  
    11  import (
    12  	"bufio"
    13  	"bytes"
    14  	"io"
    15  	"os"
    16  )
    17  
    18  type MyInt int
    19  
    20  var i int
    21  var j MyInt
    22  
    23  // STOP OMIT
    24  
    25  // Reader is the interface that wraps the basic Read method.
    26  type Reader interface {
    27  	Read(p []byte) (n int, err error)
    28  }
    29  
    30  // Writer is the interface that wraps the basic Write method.
    31  type Writer interface {
    32  	Write(p []byte) (n int, err error)
    33  }
    34  
    35  // STOP OMIT
    36  
    37  func readers() { // OMIT
    38  	var r io.Reader
    39  	r = os.Stdin
    40  	r = bufio.NewReader(r)
    41  	r = new(bytes.Buffer)
    42  	// and so on
    43  	// STOP OMIT
    44  }
    45  
    46  func typeAssertions() (interface{}, error) { // OMIT
    47  	var r io.Reader
    48  	tty, err := os.OpenFile("/dev/tty", os.O_RDWR, 0)
    49  	if err != nil {
    50  		return nil, err
    51  	}
    52  	r = tty
    53  	// STOP OMIT
    54  	var w io.Writer
    55  	w = r.(io.Writer)
    56  	// STOP OMIT
    57  	var empty interface{}
    58  	empty = w
    59  	// STOP OMIT
    60  	return empty, err
    61  }
    62  
    63  func main() {
    64  }