当前位置:网站首页>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)
}
边栏推荐
- Testing principle and precautions of biovendor rage ELISA Kit
- 1.微信小程序页面跳转方法总结;2. navigateTo堆栈到十层不跳转问题
- How can I get the stock account opening discount link? In addition, is it safe to open a mobile account?
- A complete collection of vulnerability scanning tools. Mom doesn't have to worry that I won't find any more vulnerabilities
- centos——开启/关闭oracle
- [original] unable to use the code dialog height and width to solve the screen problem
- Flinksql两个kafka 流可以进行join么?
- coredns 修改upstream
- Lumiprobe copper free click chemical solution
- Qiao NPMS: search for NPM packages
猜你喜欢

开发技术-使用easyexcel导入文件(简单示例)

哈夫曼树(一)基本概念与C语言实现

Go语学习笔记 - gorm使用 - 数据库配置、表新增 | Web框架Gin(七)

在线教育项目用户登录和注册

STL的基本组成部分

Failed to configure a DataSource: ‘url‘ attribute is not specified and no embedded datasource could

Lumiprobe copper free click chemical solution

Lumiprobe染料 NHS 酯丨BDP FL NHS 酯研究

qiao-npms:搜索npm包

断点续传和下载原理分析
随机推荐
防范未授权访问攻击的十项安全措施
STL的基本组成部分
北京大学ACM Problems 1004:Financial Management
uniapp-生命周期/路由跳转
vncserver: Failed command ‘/etc/X11/Xvnc-session‘: 256!
Qiao NPMS: search for NPM packages
Go learning notes
B_QuRT_User_Guide(35)
B_QuRT_User_Guide(31)
Scene 299
多表操作-外键约束
谈谈内联函数
MySQL简介、详细安装步骤及使用 | 黑马程序员
Deflection lock / light lock / heavy lock lock is healthier. How to complete locking and unlocking
DM8:生成DM AWR报告
微信小程序怎么实现圆心进度条
Label Contrastive Coding based Graph Neural Network for Graph Classification
Software engineering UML drawing
AVL balanced binary tree (I) - concept and C language implementation
Lumiprobe 改性三磷酸盐丨生物素-11-UTP研究