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

时间:2023-03-10 01:52:03
[日常] 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.Proto,r.Header,r.Host,r.RemoteAddr,r.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占满

效果图:

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

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

代码:

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 }