開発環境
- macOS Mojave - Apple (OS)
- Emacs (Text Editor)
- Windows 10 Pro (OS)
- Visual Studio Code (Text Editor)
- Go (プログラミング言語)
プログラミング言語Go (ADDISON-WESLEY PROFESSIONAL COMPUTING SERIES) (Alan A.A. Donovan(著)、Brian W. Kernighan(著)、柴田 芳樹(翻訳)、丸善出版)の第5章(関数)、5.2(再帰)、練習問題5.1の解答を求めてみる。
コード
package main import ( "fmt" "os" "golang.org/x/net/html" ) func main() { doc, err := html.Parse(os.Stdin) if err != nil { fmt.Fprintf(os.Stderr, "findlinks: %v\n", err) os.Exit(1) } for _, link := range visit(nil, doc) { fmt.Println(link) } } func visit(links []string, n *html.Node) []string { if n == nil { return links } if n.Type == html.ElementNode && n.Data == "a" { for _, a := range n.Attr { if a.Key == "href" { links = append(links, a.Val) } } } links = visit(links, n.FirstChild) links = visit(links, n.NextSibling) return links }
入出力結果(cmd(コマンドプロンプト)、Terminal)
$ ./fetch https://golang.org | ./findlinks1 / / # /doc/ /pkg/ /project/ /help/ /blog/ # # //tour.golang.org/ /dl/ //blog.golang.org/ https://developers.google.com/site-policies#restrictions https://creativecommons.org/licenses/by/3.0/ /LICENSE /doc/tos.html http://www.google.com/intl/en/policies/privacy/ $ ./fetch https://golang.org | go run sample1.go / / # /doc/ /pkg/ /project/ /help/ /blog/ # # //tour.golang.org/ /dl/ //blog.golang.org/ https://developers.google.com/site-policies#restrictions https://creativecommons.org/licenses/by/3.0/ /LICENSE /doc/tos.html http://www.google.com/intl/en/policies/privacy/ $
0 コメント:
コメントを投稿