Created
November 28, 2018 06:44
-
-
Save sezemiadmin/84510715813a3a437ff169fdcb480b84 to your computer and use it in GitHub Desktop.
はじめてのGo言語 SampleWebApp
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| package main | |
| import ( | |
| "fmt" | |
| "io" | |
| "net/http" | |
| ) | |
| func main() { | |
| //GETでアクセスし、データを取得する | |
| res, err := http.Get("https://yahoo.co.jp") | |
| if err != nil { | |
| fmt.Println("Request error:", err) | |
| return | |
| } | |
| //終了時、必ずCloseする | |
| defer res.Body.Close() | |
| //読み取るバッファの生成 | |
| buf := make([]byte, 256) | |
| for { | |
| //読み込む | |
| n, err := res.Body.Read(buf) | |
| //終了、または読み込み0まで続ける | |
| if n == 0 || err == io.EOF { | |
| break | |
| } else if err != nil { | |
| //エラー時の処理 | |
| fmt.Println("エラー発生:", err) | |
| return | |
| } | |
| //標準出力に書き出す | |
| fmt.Println(string(buf[:n])) | |
| } | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| package main | |
| import ( | |
| "fmt" | |
| "io/ioutil" | |
| "net/http" | |
| "os" | |
| "strings" | |
| ) | |
| //サーバが保持するメモのリスト | |
| var memos []string | |
| func main() { | |
| fmt.Println(os.Getwd()) | |
| //メモの初期化 | |
| memos = make([]string, 0) | |
| //memosにアクセスが来た場合、showMemo関数を呼び出す | |
| http.HandleFunc("/memos", showMemo) | |
| //memos/addにアクセスが来た場合、addMemo関数を呼び出す | |
| http.HandleFunc("/memos/new", addMemo) | |
| //リスナを起動する | |
| http.ListenAndServe(":80", nil) | |
| } | |
| // memoへのアクセス時に応答する | |
| func showMemo(w http.ResponseWriter, r *http.Request) { | |
| //index.htmlファイルを読み込み | |
| index, err := ioutil.ReadFile(`./index.html`) | |
| if err != nil { | |
| fmt.Print(err) | |
| return | |
| } | |
| //メモのリストから、表示用HTMLを生成 | |
| memo := "<li>" + strings.Join(memos, "</li><li>") + "</li>" | |
| // HTML中の[[LIST_AREA]]をmemoのHTMLに置換 | |
| ret := strings.Replace(string(index), "[[LIST_AREA]]", memo, 1) | |
| //ResponseWriterにHTMLを書き込む | |
| fmt.Fprintln(w, ret) | |
| } | |
| // memo/addへのアクセス時に応答する | |
| func addMemo(w http.ResponseWriter, r *http.Request) { | |
| //POSTされた"memo"の値を受け取る | |
| receiveValue := r.FormValue("memo") | |
| //保持しているメモリストに追加する | |
| memos = append(memos, receiveValue) | |
| //memosにリダイレクトさせる | |
| http.Redirect(w, r, "/memos", 303) | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <html> | |
| <head><title>ToDo List</title></head> | |
| <body> | |
| <h1>メモ一覧</h1> | |
| <ul> | |
| [[LIST_AREA]] | |
| </ul> | |
| <h2>メモの追加</h2> | |
| <form method="post" action="/memos/new"> | |
| <input type="text" name="memo"> | |
| <input type="submit" name="Add"> | |
| </form> | |
| </body> | |
| </html> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment