Skip to content

Commit

Permalink
update
Browse files Browse the repository at this point in the history
  • Loading branch information
fuxingZhang committed Aug 28, 2020
1 parent 2973666 commit 9b0b924
Show file tree
Hide file tree
Showing 3 changed files with 64 additions and 0 deletions.
39 changes: 39 additions & 0 deletions fetch/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
package main

import (
"fmt"
"io"
"io/ioutil"
"net/http"
"os"
"time"
)

func main() {
start := time.Now()
ch := make(chan string)
for _, url := range os.Args[1:] {
go fetch(url, ch)
}
for range os.Args[1:] {
fmt.Println(<-ch)
}
fmt.Printf("%.2fs elapsed\n", time.Since(start).Seconds())
}

func fetch(url string, ch chan<- string) {
start := time.Now()
resp, err := http.Get(url)
if err != nil {
ch <- fmt.Sprint(err)
return
}
defer resp.Body.Close()
nbytes, err := io.Copy(ioutil.Discard, resp.Body)
if err != nil {
ch <- fmt.Sprintf("while reading %s: %v", url, err)
return
}
secs := time.Since(start).Seconds()
ch <- fmt.Sprintf("%.2fs %7d %s", secs, nbytes, url)
}
7 changes: 7 additions & 0 deletions flag/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
```bash
go run .\main.go -h
go run .\main.go --help
go run .\main.go a bc zfx
go run .\main.go -s / a bc zfx
go run .\main.go -n a bc zfx
```
18 changes: 18 additions & 0 deletions flag/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package main

import (
"flag"
"fmt"
"strings"
)

var n = flag.Bool("n", false, "omit trailling newline")
var sep = flag.String("s", " ", "separator")

func main() {
flag.Parse()
fmt.Print(strings.Join(flag.Args(), *sep))
if !*n {
fmt.Println()
}
}

0 comments on commit 9b0b924

Please sign in to comment.