티스토리 뷰
# Web Server?
- Go의 표준 패키지인 net/http 패키지는 웹 관련 서버 기능을 제공함.
import (
"net/http"
)
- 간단한 예제
// HttpExam project main.go
package main
import (
"net/http"
)
func main() {
http.HandleFunc("/hello", func(w http.ResponseWriter, req *http.Request) { w.Write([]byte("Hello World")) })
http.ListenAndServe(":5000", nil)
}
HandleFunc()의 /hello Path에 대한 익명함수를 실행하여 Hello World를 실행함.
익명함수 내의
http.ResponseWrite 파라미터는 HTTP Response에 무언가를 쓸수있게 함.
http.Request 파라미터는 입력된 Request 요청을 검토할 수 있게 함(?)
http.ListenAndServe () 메서드는 2개의 파라미터를 가지며
1. 포트 지정
2. 어떤 ServeMux를 사용할 지를 지정함. (관련된 내용은 추후에 보충)
- http.Handler 인터페이스를 갖는 testHandler 라는 struct를 정의하고 이 struct의 메서드 ServeHTTP()을 구현한 예
// HttpExam project main.go
package main
import (
"net/http"
)
func main() {
// http.HandleFunc("/hello", func(w http.ResponseWriter, req *http.Request) { w.Write([]byte("Hello World")) })
http.Handle(("/"), new(testHandler))
http.ListenAndServe(":5000", nil)
}
type testHandler struct {
http.Handler
}
func (h *testHandler) ServeHTTP(w http.ResponseWriter, req *http.Request) {
str := "Your Request Path is " + req.URL.Path
w.Write([]byte(str))
}
# 참고
예제로 배우는 Go 프로그래밍 - 간단한 웹 서버 (HTTP 서버)
1. 간단한 HTTP 서버 Go의 표준 패키지인 net/http 패키지는 웹 관련 서버 (및 클라이언트) 기능을 제공한다. Go에서 HTTP 서버를 만들기 위해 중요한 http 패키지 메서드로 ListenAndServe(), Handle(), HandleFunc() 등을 들 수 있다. ListenAndServe() 메서드는 지정된 포트에 웹 서버를 열고 클라이언트 Request를 받아들여 새 Go 루틴에 작업을 할당하는 일을 한다. Handle()과 Ha
golang.site
'Study > Go Lang' 카테고리의 다른 글
[Go Lang] Mahalanobis Distance (0) | 2019.12.18 |
---|---|
[Go Lang] Euclidean Distance (0) | 2019.12.18 |
- Total
- Today
- Yesterday
- Vampire Survivor
- 이더리움 채굴기
- 이더리움
- minting
- GO
- node
- 회원 탈퇴
- P3X Redis UI
- phaser3
- nodejs
- 몽고db
- 채굴
- Linux
- 민팅
- 뱀파이어 서바이벌
- 지갑 생성
- remote-ftp
- 네이버 클라우드 플랫폼
- pharser3
- OpenSea
- go lang
- node.js
- mysql
- 뱀파이어 사바이벌
- 모니터 설정
- 비트코인
- mongodb
- krafterspace
- phaser
- pharser
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | 2 | 3 | 4 | 5 | ||
6 | 7 | 8 | 9 | 10 | 11 | 12 |
13 | 14 | 15 | 16 | 17 | 18 | 19 |
20 | 21 | 22 | 23 | 24 | 25 | 26 |
27 | 28 | 29 | 30 |