用Golang的http包建立Web服务器 案例

1、web.go

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

func sayhello(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, "welcome to richerdyoung!") //输出到客户端的信息
}

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

 

2、cmd运行web.go:go run web.go

go run web.go

 

3、打开浏览器,输入:localhost:8888

Go语言localhost:8888

    A+
发布日期:2017年09月16日 15:18:03  所属分类:Go语言
最后更新时间:2017-12-15 20:07:10
头像
  • ¥ 29.99元
  • 市场价:888元
  • ¥ 79.0元
  • 市场价:129.0元
  • ¥ 39.0元
  • 市场价:39.0元
  • ¥ 79.0元
  • 市场价:99.0元

发表评论

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen: