2020年7月14日火曜日

開発環境

Go Systems Programming: Master Linux and Unix system level programming with Go (渋川 よしき(著)、ごっちん(イラスト)、ラムダノート)、第3章(低レベルアクセスへの入り口2:io.Reader)、3.9(問題)、Q3.4(zipファイルをウェブサーバーからダウンロード)の解答を求めてみる。

コード

package main

import (
	"archive/zip"
	"fmt"
	"net/http"
	"os"
)

func main() {
	http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
		fmt.Println(r)
		w.Header().Set("Content-Type", "application/zip")
		w.Header().Set("Content-Disposition", "attachment; filename=ascii_sample.zip")
		zipWriter := zip.NewWriter(w)
		defer zipWriter.Close()
		writer, err := zipWriter.Create("ascii_sample.txt")
		if err != nil {
			fmt.Fprintln(os.Stderr, err)
		} else {
			writer.Write([]byte("Hello, 世界!"))
		}
	})
	http.ListenAndServe(":8080", nil)
}

入出力結果(Zsh、PowerShell、Terminal)

% go build
% ./download 
&{GET / HTTP/1.1 1 1 map[Accept:[text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8] Accept-Encoding:[gzip, deflate] Accept-Language:[en-us] Connection:[keep-alive] Cookie:[_ga=GA1.1.1397405368.1540279230] Upgrade-Insecure-Requests:[1] User-Agent:[Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_5) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/13.1.1 Safari/605.1.15]] {} <nil> 0 [] false localhost:8080 map[] map[] <nil> map[] [::1]:58203 / <nil> <nil> <nil> 0xc0000d8380}
&{GET / HTTP/1.1 1 1 map[Accept:[text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8] Accept-Encoding:[gzip, deflate] Accept-Language:[en-us] Connection:[keep-alive] Cookie:[_ga=GA1.1.1397405368.1540279230] Upgrade-Insecure-Requests:[1] User-Agent:[Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_5) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/13.1.1 Safari/605.1.15]] {} <nil> 0 [] false localhost:8080 map[] map[] <nil> map[] [::1]:58203 / <nil> <nil> <nil> 0xc000230000}
&{GET / HTTP/1.1 1 1 map[Accept:[text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8] Accept-Encoding:[gzip, deflate] Accept-Language:[en-us] Connection:[keep-alive] Cookie:[_ga=GA1.1.1397405368.1540279230] Upgrade-Insecure-Requests:[1] User-Agent:[Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_5) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/13.1.1 Safari/605.1.15]] {} <nil> 0 [] false localhost:8080 map[] map[] <nil> map[] [::1]:58203 / <nil> <nil> <nil> 0xc0002300c0}
&{GET / HTTP/1.1 1 1 map[Accept:[text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8] Accept-Encoding:[gzip, deflate] Accept-Language:[en-us] Connection:[keep-alive] Cookie:[_ga=GA1.1.1397405368.1540279230] Upgrade-Insecure-Requests:[1] User-Agent:[Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_5) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/13.1.1 Safari/605.1.15]] {} <nil> 0 [] false localhost:8080 map[] map[] <nil> map[] [::1]:58203 / <nil> <nil> <nil> 0xc0000d8480}
&{GET / HTTP/1.1 1 1 map[Accept:[text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8] Accept-Encoding:[gzip, deflate] Accept-Language:[en-us] Connection:[keep-alive] Cookie:[_ga=GA1.1.1397405368.1540279230] Upgrade-Insecure-Requests:[1] User-Agent:[Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_5) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/13.1.1 Safari/605.1.15]] {} <nil> 0 [] false localhost:8080 map[] map[] <nil> map[] [::1]:58209 / <nil> <nil> <nil> 0xc0000d8500}
^C
% 

0 コメント:

コメントを投稿