Source file doc/progs/interface.go

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

View as plain text