Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

go 实现一个简易的 http server #2

Open
EchoZhaoH opened this issue Aug 17, 2018 · 0 comments
Open

go 实现一个简易的 http server #2

EchoZhaoH opened this issue Aug 17, 2018 · 0 comments

Comments

@EchoZhaoH
Copy link
Owner

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)
  }
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant