当前位置:网站首页>go搭建服务器基础
go搭建服务器基础
2022-06-30 20:46:00 【出现一片乱码】
goweb应用工作原理(用于个人学习)
客户端发送请求给多路复用器,多路复用器根据请求------->处理器处理器-------->模型------->数据库
数据库找到相应的数据反馈给模型,模型将数据封装发给处理器 ,处理器给模板引擎然后生成模板

go里面有相应的http的包直接调用即可,这一点比java相对方便很多,下面有几种方式可以搭建服务器
hanglerFunc方式
package main //声明包
import ( //导入包
"fmt" //打印使用
"net/http" // http服务使用
)
//处理器
func hangler(w http.ResponseWriter, r *http.Request) {
//名称可以随意命名 函数的参数不可随意命名
fmt.Fprintln(w, "hello goweb", r.URL.Path) //写到前台页面上去
}
func main() {
http.HandleFunc("/", hangler) //更具web原理 添加路径 添加处理器 将路径和处理器产生关联
http.ListenAndServe(":8080", nil) // 端口号
}
Mux多路复用方式
package main
import (
"fmt"
"net/http"
)
func hangler(w http.ResponseWriter, r *http.Request) {
fmt.Fprintln(w, "hello goweb", r.URL.Path)
}
func main() {
mux := http.NewServeMux() //多路服务器处理请求
http.HandleFunc("/", hangler)
http.ListenAndServe(":8080", mux)
}
handle
package main //声明包
import (
"fmt" // 打印
"net/http"// http
"time"
)
type Myhangdler struct{
} //定义结构体
func (m *Myhangdler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
fmt.Fprintln(w, "通过自己创建的处理器处理请求自定义hangler")
}
func main() {
myhangdler := Myhangdler{
} //实例化接工体
server := http.Server{
Addr: ":8080", //声明端口
Handler: &myhangdler, //声明处理器
ReadTimeout: 2 * time.Second, //读取时间设置
}
server.ListenAndServe() // 应用到监听中去
//http.Handle("/myhangler", &myhangdler)
//http.ListenAndServe(":8080", nil)
}
边栏推荐
- 片荒吗?不用下载直接在线免费看的资源来了!2022年收藏夹必须有它!
- Lumiprobe protein quantitation - qudye Protein Quantitation Kit
- Lumiprobe nucleic acid quantitative qudye dsDNA br detection kit
- Qiao NPMS: search for NPM packages
- docker安装mysql
- Gartner聚焦中国低代码发展 UniPro如何践行“差异化”
- qiao-npms:搜索npm包
- PM reports work like this, and the boss is willing to give you a raise
- 学习总结
- 19.04 分配器
猜你喜欢

断点续传和下载原理分析

RP原型资源分享-购物类App

Lumiprobe核酸定量丨QuDye dsDNA BR 检测试剂盒

Lumiprobe nucleic acid quantitative qudye dsDNA br detection kit

Study on lumiprobe dye NHS ester BDP FL NHS ester

Web APIs 综合案例-Tab栏切换 丨黑马程序员

DM8:生成DM AWR报告

Amazon restricts LGBTQ related search and product sales in the United Arab Emirates

【数字IC应届生职业规划】Chap.1 IC行业产业链概述及代表企业大厂汇总

A complete collection of vulnerability scanning tools. Mom doesn't have to worry that I won't find any more vulnerabilities
随机推荐
Peking University ACM problems 1003:hangover
Testing principle and precautions of biovendor rage ELISA Kit
修改已经上线的小程序名称
uniapp-路由uni-simple-router
Qt和其它GUI库的对比
B_QuRT_User_Guide(31)
Vite2兼容低版本chrome(如搜狗80),通过polyfills处理部分需求高版本的语法
AVL balanced binary tree (I) - concept and C language implementation
有趣网站汇总
jfinal中如何使用过滤器监控Druid监听SQL执行?
STL的基本组成部分
Label Contrastive Coding based Graph Neural Network for Graph Classification
Web APIs 综合案例-Tab栏切换 丨黑马程序员
加密与解密以及OpenSSL的应用
报错FileSystemException: /datas/nodes/0/indices/gtTXk-hnTgKhAcm-8n60Jw/1/index/.es_temp_file:结构需要清理
Peking University ACM problems 1005:i think I need a houseboat
Based on the open source stream batch integrated data synchronization engine Chunjun data restore DDL parsing module actual combat sharing
有趣插件汇总
ArcMap|用字段计算器对不同类别的id赋值
北京大学ACM Problems 1000:A+B Problem