ch1/ch1-07 #139
Replies: 8 comments 13 replies
-
server2: |
Beta Was this translation helpful? Give feedback.
-
为什么使用/访问服务器,count会加3呢? |
Beta Was this translation helpful? Give feedback.
-
Exercise 1.12package main
import (
"fmt"
"image"
"image/color"
"image/gif"
"io"
"log"
"math"
"math/rand"
"net/http"
"strconv"
"sync"
)
var palette = []color.Color{color.White, color.Black, color.RGBA{0x00, 0xff, 0x00, 0x4a}, color.RGBA{0xff, 0x00, 0x00, 0xff}}
const (
whiteIndex = 0
blackIndex = 1
greenIndex = 2
redIndex = 3
)
func lissajous(cycle int, out io.Writer) {
const (
// cycles = 5
res = 0.001
size = 100
nframes = 256
delay = 8
)
if cycle == 0 {
cycle = 5
}
freq := rand.Float64() * 3.0
anim := gif.GIF{LoopCount: nframes}
phase := 0.0
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 < float64(cycle)*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), redIndex)
}
phase += 0.1
anim.Delay = append(anim.Delay, delay)
anim.Image = append(anim.Image, img)
}
gif.EncodeAll(out, &anim)
}
var mu sync.Mutex
var count int
func main() {
http.HandleFunc("/", handler)
http.HandleFunc("/count", counter)
log.Fatal(http.ListenAndServe("localhost:8000", nil))
}
func handler(w http.ResponseWriter, r *http.Request) {
mu.Lock()
count++
if err := r.ParseForm(); err != nil {
log.Print(err)
}
var cycle string
for k, v := range r.Form {
if k == "cycle" {
cycle = v[0]
}
}
cycle_int, err := strconv.Atoi(cycle)
if err != nil {
cycle_int = 5
// fmt.Fprintf(w, "%v\na valid decimal cycle argument is required, not the '%v'", err, cycle)
}
lissajous(cycle_int, w)
mu.Unlock()
}
func counter(w http.ResponseWriter, r *http.Request) {
mu.Lock()
fmt.Fprintf(w, "Count %d\n", count)
mu.Unlock()
} |
Beta Was this translation helpful? Give feedback.
-
// lissajous产生随机利萨茹图形的GIF动画
package main
import (
"image"
"image/color"
"image/gif"
"io"
"log"
"math"
"math/rand"
"net/http"
"os"
"strconv"
"time"
)
var palette = []color.Color{color.White, color.Black, color.RGBA{0, 128, 0, 255}}
const (
whiteIndex = 0 //画板中的第一种颜色 --这里是画板底色
blackIndex = 1 // 画板中的下一种颜色
blueIndex = 2
)
func main() {
rand.Seed(time.Now().UTC().UnixNano())
if len(os.Args) > 1 && os.Args[1] == "web" {
//http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
// lissajous(w)
//})
handler := func(w http.ResponseWriter, r *http.Request) {
if err := r.ParseForm(); err != nil {
log.Print(err)
}
cycles, err := strconv.Atoi(r.Form.Get("cycles")) // 字符串转int
if err != nil {
log.Print(err)
}
lissajous(w, float64(cycles))
}
http.HandleFunc("/get_gif", handler)
log.Fatal(http.ListenAndServe("localhost:8080", nil))
return
}
lissajous(os.Stdout, 5)
}
func lissajous(out io.Writer, cycles float64) {
const (
res = 0.001
size = 100
nframes = 64
delay = 8
)
freq := rand.Float64() * 3.0
anim := gif.GIF{LoopCount: nframes}
phase := 0.0
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), blueIndex) //这里最后一个参数是palette的下标吧
}
phase += 0.1
anim.Delay = append(anim.Delay, delay)
anim.Image = append(anim.Image, img)
}
gif.EncodeAll(out, &anim)
} |
Beta Was this translation helpful? Give feedback.
-
`go import ( var palette = []color.Color{color.White, color.Black} const ( func lissajous(out io.Writer, cycles float64) {
} func main() { func handler3(w http.ResponseWriter, r *http.Request) { |
Beta Was this translation helpful? Give feedback.
-
package main
import (
"image/color"
"image"
"image/gif"
"io"
"math"
"math/rand"
"time"
"net/http"
"log"
"strconv"
"fmt"
)
var palette = []color.Color{
color.Black,
color.White,
color.RGBA{0,255,0,255},
color.RGBA{255,0,0,255},
color.RGBA{0,0,255,255},
}
const (
blackIndex = 0
whiteIndex = 1
greenIndex = 2
redIndex = 3
blueIndex = 4
)
var cycles = 5.0
func lissajour(w io.Writer){
const (
size = 100
delay = 8
res = 0.001
nframe = 64
)
rand.Seed(time.Now().UTC().UnixNano())
freq := rand.Float64() * 3.0
phase := 0.0
anim := gif.GIF{LoopCount: nframe}
for i := 0; i < nframe; i++ {
rect := image.Rect(0,0,2 * size + 1, 2 * size + 1)
img := image.NewPaletted(rect, palette)
for t := 0.0; t < 2 * math.Pi * cycles; t += res {
x := math.Sin(t)
y := math.Sin(freq * t + phase)
img.SetColorIndex(size + int(x * size + 0.5), size + int(y * size + 0.5), greenIndex)
}
anim.Delay = append(anim.Delay, delay)
anim.Image = append(anim.Image, img)
phase += 0.1
}
gif.EncodeAll(w, &anim)
}
// 下面处理输入参数的问题
func main(){
http.HandleFunc("/", handler)
log.Fatal(http.ListenAndServe("localhost:8000", nil))
}
func handler(w http.ResponseWriter, r *http.Request){
if err:= r.ParseForm(); err != nil{
log.Print(err)
return
}
cyclesParam := r.Form.Get("cycles")
if cyclesParam == ""{
}else{
cycleInt, error := strconv.Atoi(cyclesParam)
if error != nil{
fmt.Printf("transferring parameter: ", error)
return
}
cycles = float64(cycleInt)
lissajour(w)
}
}``` 可以观察到对于 os.Stdout, 无法直接输出类似 %q 类型,利用 r.Form 的切片和 Get 方法获取字符串,但不适合在os.Stdout 上输出,适合直接输出到浏览器 http.ResponseWriter |
Beta Was this translation helpful? Give feedback.
-
// 修改Lissajour服务,从URL读取变量,比如你可以访问 http://localhost:8000/?cycles=20 这个URL,这样访问可以将程序里的cycles默认的5修改为20。字符串转换为数字可以调用strconv.Atoi函数。你可以在godoc里查看strconv.Atoi的详细说明。 // Server1 is a minimal "echo" server. import ( var count int func main() { func index(res http.ResponseWriter, req *http.Request) { func counter(res http.ResponseWriter, req *http.Request) { func genGif(res http.ResponseWriter, req *http.Request) { func lissajous(out io.Writer) {
} |
Beta Was this translation helpful? Give feedback.
-
ch1/ch1-07
中文版
https://gopl-zh.github.io/ch1/ch1-07.html
Beta Was this translation helpful? Give feedback.
All reactions