Skip to content
เข้าสู่ระบบ

HTTP Server Patterns

Patterns และ best practices สำหรับ HTTP server ใน Go

package main
import (
"fmt"
"net/http"
)
func main() {
http.HandleFunc("/", homeHandler)
http.HandleFunc("/api/users", usersHandler)
fmt.Println("Server starting on :8080")
http.ListenAndServe(":8080", nil)
}
func homeHandler(w http.ResponseWriter, r *http.Request) {
fmt.Fprintf(w, "Welcome!")
}
func usersHandler(w http.ResponseWriter, r *http.Request) {
switch r.Method {
case http.MethodGet:
fmt.Fprintf(w, "List users")
case http.MethodPost:
fmt.Fprintf(w, "Create user")
default:
http.Error(w, "Method not allowed", http.StatusMethodNotAllowed)
}
}

เข้าสู่ระบบเพื่อดูเนื้อหาเต็ม

ยืนยันตัวตนด้วยบัญชี Google เพื่อปลดล็อกบทความทั้งหมด