Go语言设计模式(五)
2021-02-01 13:15
标签:服务器 work content handler cli http pre ons sts Go语言设计模式(五) 标签:服务器 work content handler cli http pre ons sts 原文地址:https://www.cnblogs.com/double-W/p/12813815.htmlGenerator Pattern 生成器模式
package main
import "fmt"
func Count(start int, end int) chan int {
ch := make(chan int)
go func(ch chan int) {
for i := start; i
Parallelism Pattern 并行模式
package main
import (
"fmt"
"sync"
)
func Merge(cs ...
package main
import (
"fmt"
"log"
"net/http"
)
func isAuthorized(endpoint func(http.ResponseWriter, *http.Request)) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
fmt.Println("Checking to see if Authorized header set...")
if val, ok := r.Header["Authorized"]; ok {
fmt.Println(val)
if val[0] == "true" {
fmt.Println("Header is set! We can serve content!")
endpoint(w, r)
}
} else {
fmt.Println("Not Authorized!!")
fmt.Fprintf(w, "Not Authorized!!")
}
})
}
func homePage(w http.ResponseWriter, r *http.Request) {
fmt.Println("Endpoint Hit: homePage")
fmt.Fprintf(w, "Welcome to the HomePage!")
}
func handleRequests() {
http.Handle("/", isAuthorized(homePage))
log.Fatal(http.ListenAndServe(":8081", nil))
}
func main() {
handleRequests()
}
上一篇:数据结构(算法)