当前位置:网站首页>Go build server Foundation
Go build server Foundation
2022-06-30 20:55:00 【There is a mess of code】
goweb How it works ( For personal learning )
The client sends a request to the multiplexer , Multiplexer on request -------> Processor processor --------> Model -------> database
The database finds the corresponding data and feeds it back to the model , The model encapsulates the data and sends it to the processor , The processor gives the template engine and generates the template
go There's a corresponding http Directly call the package of , This is better than java It's a lot more convenient , There are several ways to build a server
hanglerFunc The way
package main // Declaration package
import ( // Import package
"fmt" // To print using
"net/http" // http Service usage
)
// processor
func hangler(w http.ResponseWriter, r *http.Request) {
// The name can be named at will The parameters of a function cannot be named arbitrarily
fmt.Fprintln(w, "hello goweb", r.URL.Path) // Write to the front page
}
func main() {
http.HandleFunc("/", hangler) // More web principle Add a path Add processor Associate the path with the processor
http.ListenAndServe(":8080", nil) // Port number
}
Mux Multiplexing
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() // Multiple servers process requests
http.HandleFunc("/", hangler)
http.ListenAndServe(":8080", mux)
}
handle
package main // Declaration package
import (
"fmt" // Print
"net/http"// http
"time"
)
type Myhangdler struct{
} // Defining structure
func (m *Myhangdler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
fmt.Fprintln(w, " Handle request customization through the processor you create hangler")
}
func main() {
myhangdler := Myhangdler{
} // Instantiate the connector
server := http.Server{
Addr: ":8080", // Declaration port
Handler: &myhangdler, // Declare the processor
ReadTimeout: 2 * time.Second, // Read time setting
}
server.ListenAndServe() // Apply to monitoring
//http.Handle("/myhangler", &myhangdler)
//http.ListenAndServe(":8080", nil)
}
边栏推荐
- RP原型资源分享-购物类App
- What are database OLAP and OLTP? Same and different? Applicable scenarios
- 开发技术-获取10分钟前的时间
- Study on lumiprobe modified triphosphate biotin-11-utp
- stacking集成模型预测回归问题
- 北京大学ACM Problems 1005:I Think I Need a Houseboat
- 报错:Internal error XFS_WANT_CORRUPTED_GOTO at line 1635 of file fs/xfs/libxfs/xfs_alloc.c.
- Gartner聚焦中国低代码发展 UniPro如何践行“差异化”
- PHP obtains opcode and C source code
- SQL Server 提取字符串中的纯数字
猜你喜欢
随机推荐
请问海量数据如何去取最大的K个
Document contains & conditional competition
树基本概念
19.04 distributor
No "history of blood and tears" in home office | community essay solicitation
【数字IC应届生职业规划】Chap.1 IC行业产业链概述及代表企业大厂汇总
转:用实际行动赢得别人追随
Study on lumiprobe modified triphosphate biotin-11-utp
ssh-server配置文件参数PermitRootLogin介绍
STL的基本组成部分
阿里kube-eventer mysql sink简单使用记录
Huffman Tree (1) Basic Concept and C - language Implementation
CentOS - enable / disable Oracle
sqlserver 字符串类型转换成小数或者整数类型
Qt和其它GUI库的对比
Software engineering UML drawing
[1175. prime number arrangement]
哈夫曼树(一)基本概念与C语言实现
微信小程序怎么实现圆心进度条
请指教在线开户需要什么银行卡?另外想问,现在在线开户安全么?