42 lines
975 B
Go
42 lines
975 B
Go
|
package main
|
|||
|
|
|||
|
import (
|
|||
|
"fmt"
|
|||
|
"net/http"
|
|||
|
)
|
|||
|
|
|||
|
func main() {
|
|||
|
// 设置路由和处理函数
|
|||
|
http.HandleFunc("/get-value", func(w http.ResponseWriter, r *http.Request) {
|
|||
|
// 检查请求方法是否为GET
|
|||
|
if r.Method != http.MethodGet {
|
|||
|
http.Error(w, "Invalid request method", http.StatusMethodNotAllowed)
|
|||
|
return
|
|||
|
}
|
|||
|
|
|||
|
// 从请求参数中获取"key"的值
|
|||
|
key := r.URL.Query().Get("key")
|
|||
|
if key == "" {
|
|||
|
http.Error(w, "Missing 'key' parameter", http.StatusBadRequest)
|
|||
|
return
|
|||
|
}
|
|||
|
|
|||
|
// 根据key返回相应的值(这里可以替换为实际的业务逻辑)
|
|||
|
value := fmt.Sprintf("Value for key '%s'", key)
|
|||
|
|
|||
|
// 设置响应头
|
|||
|
w.Header().Set("Content-Type", "text/plain")
|
|||
|
|
|||
|
// 返回响应值
|
|||
|
w.WriteHeader(http.StatusOK)
|
|||
|
w.Write([]byte(value))
|
|||
|
})
|
|||
|
|
|||
|
// 启动HTTP服务器
|
|||
|
fmt.Println("Server is running on http://localhost:8080")
|
|||
|
err := http.ListenAndServe(":8080", nil)
|
|||
|
if err != nil {
|
|||
|
fmt.Println("Error starting server:", err)
|
|||
|
}
|
|||
|
}
|