Go搭建第一个web服务器
来源:互联网 发布:厦门倾国倾城婚纱知乎 编辑:程序博客网 时间:2024/05/22 16:43
这里,先上代码,进行演示,然后一一进行论述。
package main
import (
"fmt"
"log"
"net/http"
"strings"
)
func main() {
http.HandleFunc("/", sayhello)
err := http.ListenAndServe(":9090", nil)
if err != nil {
log.Fatal("ListenAndServe: ", err)
}
}
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)
for k, v := range r.Form {
fmt.Println("key:", k)
fmt.Println("value:", strings.Join(v, ""))
}
fmt.Fprintf(w, "Hello Golang")
}
运行:
E:\project\go\proj\src>go run webserver.go
现在打开IE浏览器,访问URL: http://127.0.0.1:9090/
控制台输出:
OK,一个很普通的web服务器搭建成功了。
接下来对上面的代码进行逐一分析。
package main //声明该文件属于main包
/*
使用import语句导入多个外部包
fmt : Package fmt implements formatted I/O(格式化输入输出)
log : Package log implements a simple logging package.(日志包)
net/http : Package http provides HTTP client and server implementations.(用户实现http客户端和服务端)
strings : Package strings implements simple functions to manipulate UTF-8 encoded strings.(字符串操作包)
*/
import (
"fmt"
"log"
"net/http"
"strings"
)
//main函数,程序的入口处
func main {
}
//HandleFunc registers the handler function for the given pattern in the DefaultServeMux
http.HandleFunc("/", sayhello)
/*
ListenAndServe listens on the TCP network address addr and then calls Serve with handler to handle requests on incoming connections. Accepted connections are configured to enable TCP keep-alives. Handler is typically nil, in which case the DefaultServeMux is used.
*/
http.ListenAndServe(":9090", nil)
//Fatal is equivalent to Print() followed by a call to os.Exit(1)
log.Fatal("ListenAndServe: ", err)
/*
1、自定义函数sayhello必须满足格式func(ResponseWriter, *Request)才能作为参数传递给http.HandleFunc函数
2、Request : A Request represents an HTTP request received by a server or to be sent by a client.
3、ParseForm : ParseForm populates(填充) r.Form and r.PostForm.
For all requests, ParseForm parses the raw query from the URL and updates r.Form.
For POST, PUT, and PATCH requests, it also parses the request body as a form and puts the results into both r.PostForm and r.Form. Request body parameters take precedence over URL query string values in r.Form.
*/
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)
for k, v := range r.Form {
fmt.Println("key:", k)
fmt.Println("value:", strings.Join(v, ""))
}
fmt.Fprintf(w, "Hello Golang")
}
- Go搭建第一个web服务器
- 《Go语言入门》第一个Go语言Web程序——简单的Web服务器
- 《Go语言入门》第一个Go语言Web程序——简单的Web服务器
- Go搭建一个Web服务器
- Go搭建一个web服务器
- go web 第一个程序例子
- Go语言_Web_第一个Web程序
- go搭建一个简单web服务器
- 搭建一个简单的Go Web服务器
- Go Web服务开发入门(一) -- 搭建简单web服务器
- 建立一个node.js服务器(使用express搭建第一个Web环境)
- 搭建第一个struts2框架web程序
- 用Intellij搭建第一个web项目
- maven搭建第一个web项目
- 第一个go程序
- 第一个Go程序
- 第一个GO程序
- 第一个 Go 程序
- 基础练习 十六进制转十进制
- 虚拟机linux端mysql数据库无法远程访问的解决办法
- github && git 使用
- java web mvc
- HowVideoCardWork & DRM_IOCRTL
- Go搭建第一个web服务器
- CURL
- MySQL中的日期计算unix_timestamp
- linux常用命令
- 在Linux中如何利用backtrace信息解决问题
- 设置button按钮边框颜色
- android的IPC方式 总结
- <22>python学习笔记——爬虫4—多线程跑得快的虫子
- SQL笔记(1检索记录)