github.com/go4org/go4@v0.0.0-20200104003542-c7e774b10ea0/readerutil/readerutil_test.go (about)

     1  /*
     2  Copyright 2016 The Go4 Authors.
     3  
     4  Licensed under the Apache License, Version 2.0 (the "License");
     5  you may not use this file except in compliance with the License.
     6  You may obtain a copy of the License at
     7  
     8       http://www.apache.org/licenses/LICENSE-2.0
     9  
    10  Unless required by applicable law or agreed to in writing, software
    11  distributed under the License is distributed on an "AS IS" BASIS,
    12  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    13  See the License for the specific language governing permissions and
    14  limitations under the License.
    15  */
    16  
    17  package readerutil
    18  
    19  import (
    20  	"expvar"
    21  	"fmt"
    22  	"io"
    23  	"io/ioutil"
    24  	"strings"
    25  )
    26  
    27  func ExampleNewStatsReader() {
    28  	var (
    29  		// r is the io.Reader we'd like to count read from.
    30  		r  = strings.NewReader("Hello world")
    31  		v  = expvar.NewInt("read-bytes")
    32  		sw = NewStatsReader(v, r)
    33  	)
    34  	// Read from the wrapped io.Reader, StatReader will count the bytes.
    35  	io.Copy(ioutil.Discard, sw)
    36  	fmt.Printf("Read %s bytes\n", v.String())
    37  	// Output: Read 11 bytes
    38  }