Golang http 建立Web服务器
2017-10-07 19:42
295 查看
package main
import (
"fmt"
"log"
"net/http"
"strings"
)
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!") //输出到客户端的信息
}
func main() {
http.HandleFunc("/", sayhelloName) //设置访问的路由
err := http.ListenAndServe(":9090", nil) //设置监听的端口
if err != nil {
log.Fatal("ListenAndServe: ", err)
}
}
在浏览器中先后输入
http://localhost:9090 http://localhost:9090/?url_long=111&url_long=222
输出到浏览器的内容是: Hello astaxie!
服务器端的控制台的输出:
G:\Users\chenjo>go run web.go
map[]
path /
scheme
[]
map[]
path /favico
4000
n.ico
scheme
[]
map[url_long:[111 222]]
path /
scheme
[111 222]
key: url_long
val: 111222
map[]
path /favicon.ico
scheme
[]
import (
"fmt"
"log"
"net/http"
"strings"
)
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!") //输出到客户端的信息
}
func main() {
http.HandleFunc("/", sayhelloName) //设置访问的路由
err := http.ListenAndServe(":9090", nil) //设置监听的端口
if err != nil {
log.Fatal("ListenAndServe: ", err)
}
}
在浏览器中先后输入
http://localhost:9090 http://localhost:9090/?url_long=111&url_long=222
输出到浏览器的内容是: Hello astaxie!
服务器端的控制台的输出:
G:\Users\chenjo>go run web.go
map[]
path /
scheme
[]
map[]
path /favico
4000
n.ico
scheme
[]
map[url_long:[111 222]]
path /
scheme
[111 222]
key: url_long
val: 111222
map[]
path /favicon.ico
scheme
[]
相关文章推荐
- 【go】用Golang的 http 包建立 Web 服务器
- golang 建立web服务器 http包源码详解
- golang 建立web服务器 http包源码详解
- 使用go的http包建立一个简单的web 服务器
- 使用Golang 搭建http web服务器
- 使用Golang 搭建http web服务器
- HttpWebRequest加载证书请求远端https服务器时 : 基础连接已经关闭: 无法与远程服务器建立信任关系
- golang web开发框架httprouter:开发简单的 REST API 服务器
- 使用Golang 搭建http web服务器
- golang web服务器处理前端HTTP请求跨域的方法
- 用vs.net新建web项目时提示错误“尝试创建web项目或打开位与http://localhost/webapplication1的web项目时无法与服务器建立连接”
- 用HttpListener 建立web服务器 实现网页浏览、文件上传
- 使用golang 搭建http web服务器[待编辑]
- 【HTTP】Web服务器工作流程小结--从建立连接到日志记录
- 使用Golang 搭建http web服务器
- Web服务器、应用程序服务器、HTTP服务器区别
- 用IIS建立高安全性Web服务器
- 用IIS建立高安全性Web服务器
- 5 与HTTP协作的web服务器
- 一种基于 HTTP 长连接的“服务器推”技术在web端的应用