码迷,mamicode.com
首页 > Web开发 > 详细

go编写简单的web服务器

时间:2015-05-02 18:05:40      阅读:129      评论:0      收藏:0      [点我收藏+]

标签:

技术分享
package main

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

//http://localhost:9090/?url_long=111&url_long=222
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 astaxie!") //这个写入到w的是输出到客户端的
}

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

 访问路径:

http://localhost:9090/?url_long=111&url_long=222

技术分享

go编写简单的web服务器

标签:

原文地址:http://www.cnblogs.com/super-d2/p/4472262.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!