当前位置:网站首页>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)
}
边栏推荐
- Huffman Tree (1) Basic Concept and C - language Implementation
- 北京大学ACM Problems 1003:Hangover
- Amazon restricts LGBTQ related search and product sales in the United Arab Emirates
- Go语学习笔记 - gorm使用 - 数据库配置、表新增 | Web框架Gin(七)
- 学习总结
- uniapp-第三方网络请求
- uniapp-生命周期/路由跳转
- 北京大学ACM Problems 1002:487-3279
- The newly born robot dog can walk by himself after rolling for an hour. The latest achievement of Wu Enda's first disciple
- 左值引用和右值引用
猜你喜欢

A complete collection of vulnerability scanning tools. Mom doesn't have to worry that I won't find any more vulnerabilities

Huffman Tree (1) Basic Concept and C - language Implementation

Basic concepts of tree

企业保护 API 安全迫在眉睫

Study on PEGylation of lumiprobe and PEG linker - iodine-peg3-acid

Label Contrastive Coding based Graph Neural Network for Graph Classification

Lumiprobe copper free click chemical solution

Analysis of breakpoint continuation and download principle

uniapp-生命周期/路由跳转

PHP obtains opcode and C source code
随机推荐
Qt和其它GUI库的对比
Go learning notes
How can I get the stock account opening discount link? In addition, is it safe to open a mobile account?
Playwright - scroll bar operation
CentOS - enable / disable Oracle
Binary search tree (1) - concept and C language implementation
[原创]用代码dialog 高度 宽度无法屏幕屏幕问题
判断js对象是否为空的方式
Analysis of breakpoint continuation and download principle
uniapp-第三方网络请求
利用日志服务器输出各种apache的日志的TOPN
Huffman Tree (1) Basic Concept and C - language Implementation
stacking集成模型预测回归问题
oprator-1初识oprator
Lumiprobe 聚乙二醇化和 PEG 接头丨碘-PEG3-酸研究
C文件指针
Lumiprobe 改性三磷酸盐丨生物素-11-UTP研究
Web APIs 综合案例-Tab栏切换 丨黑马程序员
PHP require/include differences
Scene 299