github.com/neugram/ng@v0.0.0-20180309130942-d472ff93d872/eval/testdata/method2.ng (about)

     1  // Define a new type, myReader, as a struct with a field and a method.
     2  methodik myReader struct { Source string } {
     3  	func (r) Read(b []byte) (int, error) {
     4  		n := copy(b, r.Source)
     5  		r.Source = r.Source[n:]
     6  		if n == 0 {
     7  			import "io"
     8  			return 0, io.EOF
     9  		}
    10  		return n, nil
    11  	}
    12  }
    13  
    14  r := &myReader{"OK"}
    15  
    16  import "io/ioutil"
    17  print(string(ioutil.ReadAll(r)))