加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 大数据 > 正文

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

发布时间:2020-12-16 19:35:50 所属栏目:大数据 来源:网络整理
导读:Go 1.Web 2.main 3. 4. 5. 6. 7. 1.12 解答:1.引入两个包,log,net/http2.安装godoc,apt install golang-golang-x-tools,查找某个函数用法godoc strconv 3.函数有多个返回值时,multiple-value strconv.Atoi() in single-value context ,不需要的参数要用_

Go

1.Web

2.main

3.

4.

5.

6.

7.

1.12

解答:1.引入两个包,log,net/http2.安装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占满

效果图:

代码:

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,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,64)
//fmt.Fprintf(w,"%q:%d",k,cycles);
//调用gif图形函数
if cycles < 50 {
lissajous(w,cycles)
}
} else {
fmt.Fprintf(w,"URL PATH:%q
",r.URL.Path)
fmt.Fprintf(w,"%q:%q",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,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,&amp;anim) // NOTE: ignoring encoding errors

}

  

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读