Browse Source

部分更新

v1
AICells 5 years ago
parent
commit
4be76b3ad0
7 changed files with 353 additions and 0 deletions
  1. +95
    -0
      src/learn/1.txt
  2. +91
    -0
      src/learn/2.txt
  3. +11
    -0
      src/learn/boiling.go
  4. +29
    -0
      src/learn/dup3.go
  5. +23
    -0
      src/learn/fetch.go
  6. +41
    -0
      src/learn/fetch2.go
  7. +63
    -0
      src/learn/liss.go

+ 95
- 0
src/learn/1.txt View File

@ -0,0 +1,95 @@
1
1
1
1
1
1
1
3
3
3
3
4
3
4
5
5
sssssssssssssssssssssssss
sssssssssssssssssssssssss
ttttttttttttttttttt
ttttttttttttttttttt
4r
ew
rw
f
ds
f
dsf
d
f
asf
sad
fd
sf
d
sfa
ads
f
saf
ds
f
dsa
f
ds
f
dsa
f
dsa
fd
sf
ds
f
dsa
f
dsf
dsa
f
d
af
ds
af
ds
af
dsf
s
f
sdaf
sd
afa
sf
af
d
f
sf
sa
fas
f
ds
fs
af
as
f
sdf
sa
fs
df
sf
s
fs
adfas
f
dsa
fd
saf
sa
fas

+ 91
- 0
src/learn/2.txt View File

@ -0,0 +1,91 @@
1
1
1
1
1
1
1
3
3
3
3
4
3
4
5
5
4r
ew
rw
f
ds
f
dsf
d
f
asf
sad
fd
sf
d
sfa
ads
f
saf
ds
f
dsa
f
ds
f
dsa
f
dsa
fd
sf
ds
f
dsa
f
dsf
dsa
f
d
af
ds
af
ds
af
dsf
s
f
sdaf
sd
afa
sf
af
d
f
sf
sa
fas
f
ds
fs
af
as
f
sdf
sa
fs
df
sf
s
fs
adfas
f
dsa
fd
saf
sa
fas

+ 11
- 0
src/learn/boiling.go View File

@ -0,0 +1,11 @@
package main
import "fmt"
const boilingF = 212.0
func main(){
var F = boilingF
var c = (F - 32) * 5 /9
fmt.Printf("boiling point = %g F or %g C\n", F, c)
}

+ 29
- 0
src/learn/dup3.go View File

@ -0,0 +1,29 @@
package main
import (
"fmt"
"io/ioutil"
"os"
"strings"
)
func main() {
counts := make(map[string]int)
for _, filename := range os.Args[1:] {
data, err := ioutil.ReadFile(filename)
if err != nil {
fmt.Fprintf(os.Stderr, "dup3: %v\n", err)
continue
}
for _, line := range strings.Split(string(data), "\n") {
fmt.Printf("YYY1111: %v \n", line)
counts[line]++
}
}
for line, n := range counts {
if n > 1 {
fmt.Printf("YYY: %v \t %d \n", line, n)
}
}
}

+ 23
- 0
src/learn/fetch.go View File

@ -0,0 +1,23 @@
package main
import(
"fmt"
"io/ioutil"
"net/http"
"os"
)
func main(){
for _, url := range os.Args[1:]{
resp, err := http.Get(url)
if err != nil{
fmt.Fprintf(os.Stderr, "fetch:%v\n", err)
os.Exit(1)
}
b, err := ioutil.ReadAll(resp.Body)
resp.Body.Close()
if err != nil{
fmt.Fprintf(os.Stderr, "fetch: reading %s: %v\n", url, err)
os.Exit(1)
}
fmt.Printf("IMY****\n %s\n", b)
}
}

+ 41
- 0
src/learn/fetch2.go View File

@ -0,0 +1,41 @@
package main
import(
"fmt"
"io"
"io/ioutil"
"net/http"
"os"
"time"
)
func main() {
start := time.Now()
ch := make(chan string)
for _, url := range os.Args[1:]{
go fetch(url, ch) // 启动一个goroutine
}
for range os.Args[1:]{
fmt.Println(<- ch)
}
fmt.Printf("IMY*****main* %2fs elapsed\n", time.Since(start).Seconds())
}
func fetch(url string, ch chan <- string){
start := time.Now()
resp, err := http.Get(url)
if err != nil{
ch <- fmt.Sprint(err) // 发送到通道ch
return
}
nbytes, err := io.Copy(ioutil.Discard, resp.Body)
resp.Body.Close() //不要泄露资源
if err != nil{
ch <- fmt.Sprintf("while reading %s %v", url, err)
return
}
secs := time.Since(start).Seconds()
ch <- fmt.Sprintf("IMY***fetch* %.2fs, %7d %s ", secs, nbytes, url)
}

+ 63
- 0
src/learn/liss.go View File

@ -0,0 +1,63 @@
package main
import (
"image"
"image/color"
"image/gif"
"io"
"log"
"math"
"math/rand"
"net/http"
"os"
"time"
)
var palette = []color.Color{color.White, color.Black}
const (
whiteIndex = 0
blackIndex = 1
)
func main() {
rand.Seed(time.Now().UTC().UnixNano())
if len(os.Args) > 1 && os.Args[1] == "web" {
handler := func(w http.ResponseWriter, r *http.Request) {
liss(w)
}
http.HandleFunc("/", handler)
log.Fatal(http.ListenAndServe("localhost:8000", nil))
return
}
liss(os.Stdout)
}
func liss(out io.Writer) {
const (
cycles = 5
res = 0.0001
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), blackIndex)
}
phase += 0.1
anim.Delay = append(anim.Delay, delay)
anim.Image = append(anim.Image, img)
}
gif.EncodeAll(out, &anim)
}

Loading…
Cancel
Save