funcpageLinks(links []string, n *html.Node) []string { if n.Type == html.ElementNode && n.Data == "a" { for _, a := range n.Attr { if a.Key == "href" { links = append(links, a.Val) } } } for c := n.FirstChild; c != nil; c = c.NextSibling { links = pageLinks(links, c) } return links }
$ docker run golang go get -v github.com/flaviocopes/findlinks github.com/flaviocopes/findlinks (download) Fetching https://golang.org/x/net/html?go-get=1 Parsing meta tags from https://golang.org/x/net/html?go-get=1 (status code 200) get "golang.org/x/net/html": found meta tag main.metaImport{Prefix:"golang.org/x/net", VCS:"git", RepoRoot:"https://go.googlesource.com/net"} at https://golang.org/x/net/html?go-get=1 get "golang.org/x/net/html": verifying non-authoritative meta tag Fetching https://golang.org/x/net?go-get=1 Parsing meta tags from https://golang.org/x/net?go-get=1 (status code 200) golang.org/x/net (download) golang.org/x/net/html/atom golang.org/x/net/html github.com/flaviocopes/findlinks
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 343d96441f16 golang "go get -v github...." 3 minutes ago Exited (0) 2 minutes ago mystifying_swanson
Page = "flaviocopes.com" Link = "https://flaviocopes.com/index.xml" Link = "https://twitter.com/flaviocopes" Link = "https://github.com/flaviocopes" Link = "https://stackoverflow.com/users/205039/flaviocopes" Link = "https://linkedin.com/in/flaviocopes/" Link = "mailto:[[email protected]](/cdn-cgi/l/email-protection)" Link = "/" Link = "/page/contact/" Link = "/page/about/" Link = "https://flaviocopes.com/golang-tutorial-rest-api/" Link = "https://flaviocopes.com/golang-environment-variables/" Link = "https://flaviocopes.com/golang-sql-database/" Link = "https://flaviocopes.com/golang-is-go-object-oriented/" Link = "https://flaviocopes.com/golang-comparing-values/" Link = "https://flaviocopes.com/golang-data-structures/" Link = "https://flaviocopes.com/golang-data-structure-binary-search-tree/" Link = "https://flaviocopes.com/golang-data-structure-graph/" Link = "https://flaviocopes.com/golang-data-structure-linked-list/" Link = "https://flaviocopes.com/golang-data-structure-queue/" Link = "https://flaviocopes.com/golang-data-structure-stack/" Link = "https://flaviocopes.com/golang-event-listeners/"
為什麼映像檔這麼大?因為 Go 應用程式是在容器內編譯的。因此映像檔需要安裝 Go 編譯器。當然還需要編譯器所需的所有東西,如 GCC,以及整個 Linux 發行版(Debian Jessie)。它下載並安裝 Go,編譯應用程式並運行,這一切都是如此快速,我們甚至都沒有注意到。但我們可以做得更好。我們該怎麼做?我們使用了 Nick Gauthier 在 為 Go 應用程式構建最小的 Docker 容器 文章學到的東西。
docker run --rm -it -p 8000:8000 flaviocopes/golang-docker-example-findlinks
輸出結果與之前相同,但這次映像檔不是 720MB,而只有 11.1MB。
1 2 3
REPOSITORY TAG IMAGE ID CREATED SIZE flaviocopes/golang-docker-example-findlinks latest f32d2fd74638 14 minutes ago 11.1MB findlinks latest c60f6792b9f3 20 minutes ago 720MB
FROM golang:1.8.3 as builder WORKDIR /go/src/github.com/flaviocopes/findlinks RUN go get -d -v golang.org/x/net/html COPY findlinks.go . RUN CGO_ENABLED=0 GOOS=linux go build -a -installsuffix cgo -o findlinks .
FROM alpine:latest RUN apk --no-cache add ca-certificates WORKDIR /root/ COPY --from=builder /go/src/github.com/flaviocopes/findlinks/findlinks . CMD ["./findlinks"]