EchoZhaoH / notes

写一写工作中遇到的问题以及解决方案

Home Page:https://echozw.github.io/

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

go 实现一个简易的 http server

EchoZhaoH opened this issue · comments

commented
package main

import (
  "fmt"
  "net/http"
  "strings"
  "log"
)

func sayHelloName(w http.ResponseWriter, r *http.Request) {
  r.ParseForm() // 解析参数
  fmt.Println(r.Form) // 输出到服务端的打印信息
  fmt.Println("path", r.URL.Path)
  fmt.Println("scheme", r.URL.Scheme)
  fmt.Println(r.Form["url_long"])
  for k, v := range r.Form {
    fmt.Println("key: ", k)
    fmt.Println("val: ", strings.Join(v, ""))
  }
  fmt.Fprintf(w, "Hello World") // 写入到 w 输出到客户端
}

func main() {
  http.HandleFunc("/", sayHelloName) // 设置访问路由
  err := http.ListenAndServe(":9090", nil) // 设置监听端口
  if err != nil {
    log.Fatal("ListenAndServer: ", err)
  }
}