[日常] Go语言圣经-WEB服务与习题

Go语言圣经-web服务

1.Web服务程序,标准库里的方法已经帮我们完成了大量工作

2.main函数将所有发送到/路径下的请求和handler函数关联起来,/开头的请求其实就是所有发送到当前站点上的请求,服务监听8000端口

3.发送到这个服务的“请求”是一个http.Request类型的对象,这个对象中包含了请求中的一系列相关字段,其中就包括我们需要的URL

4.用标准输出流fmt.Fprintf,把其发送到响应中,直接写到了http.ResponseWriter

5.两个请求处理函数,根据请求的url不同会调用不同的函数,服务器每一次接收请求处理时都会另起一个goroutine,这样服务器就可以同一时间处理多个请求

6.解决并发引起的严重bug:竞态条件,使用了mu.Lock()mu.Unlock()

7.把请求的http头和请求的form数据都打印出来,r.Method, r.URL, r.Protor.Headerr.Hostr.RemoteAddrr.Form

 

练习 1.12: 修改Lissajour服务,从URL读取变量,比如你可以访问 http://localhost:8000/?cycles=20 这个URL,这样访问可以将程序里的cycles默认的5修改为20。字符串转换为数字可以调用strconv.Atoi函数。你可以在godoc里查看strconv.Atoi的详细说明。

 

解答:
1.引入两个包,log,net/http
2.安装godoc , apt install golang-golang-x-tools , 查找某个函数用法godoc strconv
3.函数有多个返回值时,multiple-value strconv.Atoi() in single-value context ,不需要的参数要用_接收
4.Lissajour服务里面的cycles参数要是float64类型,使用cycles,_ := strconv.ParseFloat(v[0],64)函数转换
5.不能有任何输出语句,否则会弹出下载框
6.传参不能太大,要进行一下判断,否则CPU占满

效果图:

代码:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
package main
 
import (
        "fmt"
        "image"
        "image/color"
        "image/gif"
        "math"
        "math/rand"
         
        "log"
        "net/http"
        "strconv"
)
 
//定义一个slice切片变量,复合声明
var palette = []color.Color{color.White, color.RGBA{0, 255, 68, 255}, color.RGBA{26, 0, 255, 255}}
 
//声明一组常量
const (
        whiteIndex = 0
        blackIndex = 1
)
 
func main() {
        http.HandleFunc("/", handler)
        log.Fatal(http.ListenAndServe("0.0.0.0:8000", nil))
        fmt.Println("hello")
}
 
//处理http请求
func handler(w http.ResponseWriter, r *http.Request) {
        //if语句中嵌套表达式
        if err := r.ParseForm(); err != nil {
                log.Print(err)
        }  
        for k, v := range r.Form {
                if k == "cycles" {
                        //函数有多个返回值时,multiple-value strconv.Atoi() in single-value context ,不需要的参数要用_接收
                        cycles, _ := strconv.ParseFloat(v[0], 64)
                        //fmt.Fprintf(w,"%q:%d",k,cycles);
                        //调用gif图形函数
                        if cycles < 50 {
                                lissajous(w, cycles)
                        }  
                } else {
                        fmt.Fprintf(w, "URL PATH:%q <br/>", r.URL.Path)
                        fmt.Fprintf(w, "%q:%q", k, v)
                }  
        }  
}
 
//gif图形函数
func lissajous(out http.ResponseWriter, cycles float64) {
        const (
                //cycles  = 5     // number of complete x oscillator revolutions
                res     = 0.001 // angular resolution
                size    = 100   // image canvas covers [-size..+size]
                nframes = 64    // number of animation frames
                delay   = 8     // delay between frames in 10ms units
        )
 
        freq := rand.Float64() * 3.0 // relative frequency of y oscillator
        anim := gif.GIF{LoopCount: nframes}
        phase := 0.0 // phase difference
        for i := 0; i < nframes; i++ {
                rect := image.Rect(0, 0, 2*size+1, 2*size+1)
                img := image.NewPaletted(rect, palette)
                for t := 0.0; t < cycles*2*math.Pi; t += res {
                        x := math.Sin(t)
                        y := math.Sin(t*freq + phase)
                        img.SetColorIndex(size+int(x*size+0.5), size+int(y*size+0.5),
                                blackIndex)
                }
                phase += 0.1
                anim.Delay = append(anim.Delay, delay)
                anim.Image = append(anim.Image, img)
        }
        gif.EncodeAll(out, &anim) // NOTE: ignoring encoding errors
 
}

  

 

posted @   唯一客服系统开发笔记  阅读(633)  评论(0编辑  收藏  举报
编辑推荐:
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
阅读排行:
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· 开源Multi-agent AI智能体框架aevatar.ai,欢迎大家贡献代码
· Manus重磅发布:全球首款通用AI代理技术深度解析与实战指南
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· AI技术革命,工作效率10个最佳AI工具
历史上的今天:
2017-04-07 [Linux] PHP程序员玩转Linux系列-Nginx中的HTTPS
2016-04-07 [android] 手机卫士自定义控件的属性
2016-04-07 [android] 手机卫士关闭自动更新
2016-04-07 [MongoDB] MongoDB增删查改
点击右上角即可分享
微信分享提示
1
chat with us