Web Handler

go 2022. 4. 14. 13:41

출처 : https://www.youtube.com/watch?v=4Oml8mbBXgo&ab_channel=TuckerProgramming 

https://github.com/tuckersGo/goWeb/blob/master/web1/main.go

위 코드로 공부겸 샘플 예제 실습

 

package main

import (
"fmt"
"net/http"
)

type fooHandler struct{}

func (f *fooHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
fmt.Fprint(w, "Hello Foo!")
}

func main() {
http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
fmt.Fprint(w, "Hello World")
})

http.HandleFunc("/bar", func(w http.ResponseWriter, r *http.Request) {
fmt.Fprintf(w, "Hello Bar!")
})

http.Handle("/foo", &fooHandler{})

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

 

인덱스 경로 

http://192.168.0.6:3000/

/bar 경로 

http://192.168.0.6:3000/bar

/foo 경로 fooHandler인스턴스 메서드 실행 

http://192.168.0.6:3000/foo

 

'go' 카테고리의 다른 글

cryptopunks raritysniper json 파싱  (0) 2022.04.15
JSON Transfer  (0) 2022.04.14
go언어 설치  (0) 2022.04.14
routing module  (0) 2022.04.14
golang 개발환경 구축  (0) 2021.06.21
블로그 이미지

iesay

,