0
0

More than 3 years have passed since last update.

「はじめてのGo言語」でhttpパッケージが動かなかったので、ググってみた

Posted at

「はじめてのGo言語を読んで、httpパッケージサンプルが動かなかったので、調べてみました。

動かなかった原因はHandelFuncが、ConnではなくResponseWriterを使うようになったからなのかなぁ。

http.go
package main                                                                    

import (
    "io"
    "net/http"
)

// ResponseWriter is interface as
//  Header()  Header
//  Write([]byte)(int,error)
//  iWriteHeder(statuscoe int)

// WriteString is function as
// func WriteString(w Writer, s string) (n int, err error)

// Writer is interface as
// Write(p []byte) (n int, err error)

// 原著で動かない部分
// func ServeHoge(conn *http.Conn, req *http.Request){
//     io.WriteString(conn, "hello")
//

func ServeHoge(w http.ResponseWriter, _ *http.Request) {
    io.WriteString(w, "hello! this is hoge server\n")
}

func main() {
    http.HandleFunc("/hoge", ServeHoge)

    http.ListenAndServe(":12345", nil)
}
0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0