Skip to content

Latest commit

 

History

History
61 lines (46 loc) · 885 Bytes

net_http_package.md

File metadata and controls

61 lines (46 loc) · 885 Bytes

net/http Package

net/http

Hello World 예제

package main

import (
	"net/http"
)

func main() {
	http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
		w.Write([]byte("Hello World!!"))
	})

	http.ListenAndServe(":8080", nil)
}

결과

$ curl localhost:8080
Hello World!!

Handler 사용

package main

import (
	"io"
	"net/http"
)

func main() {
	h1 := func(w http.ResponseWriter, r *http.Request) {
		io.WriteString(w, "Hello from a HandleFunc #1!\n")
	}
	h2 := func(w http.ResponseWriter, r *http.Request) {
		io.WriteString(w, "Hello from a HandleFunc #2!\n")
	}

	http.HandleFunc("/", h1)
	http.HandleFunc("/endpoint", h2)
	
	http.ListenAndServe(":8080", nil)
}

결과

$ curl localhost:8080
Hello from a HandleFunc #1!

$ curl localhost:8080/endpoint
Hello from a HandleFunc #2!