Created
July 10, 2014 12:56
-
-
Save bodokaiser/4f32be93a753c4b67a80 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import ( | |
"bufio" | |
"fmt" | |
"io" | |
"log" | |
"net/http" | |
"time" | |
) | |
func main() { | |
err := request("http://www.google.com") | |
if err != nil { | |
log.Fatal(err) | |
} | |
// just keep main alive with sleep for now | |
time.Sleep(2 * time.Second) | |
} | |
func request(url string) error { | |
res, err := http.Get(url) | |
if err != nil { | |
return err | |
} | |
go scanLineWise(res.Body) | |
go scanWordWise(res.Body) | |
return err | |
} | |
func scanLineWise(r io.Reader) { | |
s := bufio.NewScanner(r) | |
s.Split(bufio.ScanLines) | |
i := 0 | |
for s.Scan() { | |
i++ | |
} | |
fmt.Printf("Counted %d lines.\n", i) | |
} | |
func scanWordWise(r io.Reader) { | |
s := bufio.NewScanner(r) | |
s.Split(bufio.ScanLines) | |
i := 0 | |
for s.Scan() { | |
i++ | |
} | |
fmt.Printf("Counted %d words.\n", i) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment