2019年1月12日土曜日

開発環境

プログラミング言語Go (ADDISON-WESLEY PROFESSIONAL COMPUTING SERIES) (Alan A.A. Donovan(著)、Brian W. Kernighan(著)、柴田 芳樹(翻訳)、丸善出版)の第1章(チュートリアル)、1.6(URLからの並行な取得)、練習問題1.10の解答を求めてみる。

コード

package main

import (
 "fmt"
 "io"
 "net/http"
 "os"
 "strconv"
 "strings"
 "time"
)

func main() {
 start := time.Now()
 ch := make(chan string)
 for i, url := range os.Args[2:] {
  if !(strings.HasPrefix(url, "http://") ||
   strings.HasPrefix(url, "https://")) {
   url = "http://" + url
  }
  n, err := strconv.Atoi(os.Args[1])
  if err != nil {
   fmt.Println(err)
   os.Exit(1)
  }
  go fetch(n+i, url, ch)
 }
 for range os.Args[2:] {
  fmt.Println(<-ch)
 }
 fmt.Printf("%.2fs elapsed\n", time.Since(start).Seconds())
}

func fetch(n int, url string, ch chan<- string) {
 start := time.Now()
 resp, err := http.Get(url)
 if err != nil {
  ch <- fmt.Sprint(err)
  return
 }
 filename := fmt.Sprintf("sample10_%d.html", n)
 file, err := os.Create(filename)
 if err != nil {
  ch <- fmt.Sprint(err)
  return
 }
 nbytes, err := io.Copy(file, resp.Body)
 resp.Body.Close()
 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)
}

入出力結果(Terminal, cmd(コマンドプロンプト))

$ go run sample10.go 0 yahoo.co.jp yahoo.com
0.28s   18560 http://yahoo.co.jp
2.23s  530375 http://yahoo.com
2.23s elapsed
$ go run sample10.go 2 yahoo.co.jp yahoo.com
0.29s   18560 http://yahoo.co.jp
1.86s  530108 http://yahoo.com
1.86s elapsed
$ diff sample10_0.html sample10_2.html 
$ diff sample10_1.html sample10_3.html | head
630c630
<             rapidConfig: {"compr_type":"deflate","tracked_mods":[],"spaceid":2023538075,"click_timeout":300,"track_right_click":true,"apv":true,"apv_time":0,"async_all_clicks":false,"compr_on":true,"yql_host":"udc.yahoo.com","yql_path":"\/v2\/public\/yql","test_id":"201","client_only":0,"pageview_on_init":true,"perf_navigationtime":2,"addmodules_timeout":500,"extendbcookie":false,"keys":{"_rid":"36marele3jkk5","mrkt":"us","pt":"home","site":"fp","ver":"megastrm","uh_vw":0,"colo":"gq1","navtype":"server","nob":1,"abk":""},"dwell_on":true,"viewability":true,"tracked_mods_viewability":{"applet_p_50000291":"featurebar","applet_p_50000287":"featurebar","applet_p_50000353":"app-wea","applet_p_50000298":"app-scor","applet_p_50000349":"storyswarm","applet_p_50000347":"rr-promo","applet_p_50000295":"app-hor"}},
---
>             rapidConfig: {"compr_type":"deflate","tracked_mods":[],"spaceid":2023538075,"click_timeout":300,"track_right_click":true,"apv":true,"apv_time":0,"async_all_clicks":false,"compr_on":true,"yql_host":"udc.yahoo.com","yql_path":"\/v2\/public\/yql","test_id":"201","client_only":0,"pageview_on_init":true,"perf_navigationtime":2,"addmodules_timeout":500,"extendbcookie":false,"keys":{"_rid":"6pdlbmpe3jkkb","mrkt":"us","pt":"home","site":"fp","ver":"megastrm","uh_vw":0,"colo":"gq1","navtype":"server","nob":1,"abk":""},"dwell_on":true,"viewability":true,"tracked_mods_viewability":{"applet_p_50000291":"featurebar","applet_p_50000287":"featurebar","applet_p_50000353":"app-wea","applet_p_50000298":"app-scor","applet_p_50000349":"storyswarm","applet_p_50000347":"rr-promo","applet_p_50000295":"app-hor"}},
1159c1159
<     data-wf-url="/tdv2_fp/remote?m_id=tdv2-applet-cards&amp;m_mode=json&amp;config.xhrPathPrefix=%2Ftdv2_fp%2Fremote&ctrl=UserIntentDiscover&config.checkTooltip=true&config.crumb=Dqr8pAL4P4u"
---
>     data-wf-url="/tdv2_fp/remote?m_id=tdv2-applet-cards&amp;m_mode=json&amp;config.xhrPathPrefix=%2Ftdv2_fp%2Fremote&ctrl=UserIntentDiscover&config.checkTooltip=true&config.crumb=U9nfQtpWPJI"
1215c1215
<             data-wf-url="/tdv2_fp/remote?m_id=tdv2-applet-cards&amp;m_mode=json&amp;config.xhrPathPrefix=%2Ftdv2_fp%2Fremote&config.crumb=Dqr8pAL4P4u&config.view=overlay&config.publisher=Yahoo%20Lifestyle&config.publisherIds=yahoo_lifestyle_pop_culture_150&config.uuid=5917fdc4-305c-30cc-b769-a45ad7f4c230&config.cpos=2&ctrl=UserIntentPanel"
$ 

キャッシュされてない、すなわち毎回同じ内容を得てはいないみたい。

0 コメント:

コメントを投稿