compress/gzipサンプル

コード

package main

import (
	"bufio"
	"compress/gzip"
	"fmt"
	"log"
	"net/http"
	"regexp"
)

const url = `http://ftp.riken.jp/lang/CPAN/authors/01mailrc.txt.gz`

var re = regexp.MustCompile(`SYOHEX\s+"([^"]+)"`)

func main() {
	res, err := http.Get(url)
	if err != nil {
		log.Fatalln(err.Error())
	}
	defer res.Body.Close()

	reader, err := gzip.NewReader(res.Body)
	if err != nil {
		log.Fatalln(err.Error())
	}

	scanner := bufio.NewScanner(reader)
	for scanner.Scan() {
		line := scanner.Text()
		if matched := re.FindStringSubmatch(line); matched != nil {
			fmt.Printf("%s\n", matched[1])
		}
	}
	if err := scanner.Err(); err != nil {
		log.Fatalln(err.Error())
	}
}

結果

% go run gzip.go
Syohei Yoshida <syohex@cpan.org>