master
dugulingping 2022-10-22 02:15:13 +08:00
parent 51829d8ada
commit a71e0b4816
19 changed files with 328 additions and 0 deletions

3
.gitignore vendored Normal file
View File

@ -0,0 +1,3 @@
/.idea
/.gitignore

BIN
day1/day-1 Executable file

Binary file not shown.

15
day1/day-1.go Normal file
View File

@ -0,0 +1,15 @@
package main
import "fmt"
func main() {
fmt.Println("hello")
}
//1.修改 echo 程序,使其能够打印 os.Args[0],即被执行命令本身的名字。
//切片第一个参数改为0
//2.练习 1.2 修改 echo 程序,使其打印每个参数的索引和值,每个一行。
//空格改为 控制符 \n
//3.练习 1.3 做实验测量潜在低效的版本和使用了 strings.Join 的版本的运行时间差异。1.6 节讲解了部分 time 包11.4 节展示了如何写标准测试程序,以得到系统性的性能评测。)

15
day1/echo1.go Normal file
View File

@ -0,0 +1,15 @@
package main
import (
"fmt"
"os"
)
func main() {
var s, sep string
for i := 1; i < len(os.Args); i++ {
s += sep + os.Args[i]
sep = " "
}
fmt.Println(s)
}

16
day1/echo2.go Normal file
View File

@ -0,0 +1,16 @@
package main
import (
"fmt"
"os"
)
func main() {
var s, sep string
for _, arg := range os.Args[1:] {
s += sep + arg
sep = " "
}
fmt.Println(s)
}

11
day1/echo3.go Normal file
View File

@ -0,0 +1,11 @@
package main
import (
"fmt"
"os"
"strings"
)
func main() {
fmt.Println(strings.Join(os.Args[0:], "\n"))
}

22
day2/dup1.go Normal file
View File

@ -0,0 +1,22 @@
package main
import (
"bufio"
"fmt"
"os"
)
func main() {
counts := make(map[string]int)
input := bufio.NewScanner(os.Stdin)
for input.Scan() {
counts[input.Text()]++
fmt.Println(counts)
}
for line, n := range counts {
if n > 1 {
fmt.Printf("%d\t%s\n", n, line)
}
}
}

BIN
day2/dup2 Executable file

Binary file not shown.

39
day2/dup2.go Normal file
View File

@ -0,0 +1,39 @@
package main
import (
"bufio"
"fmt"
"os"
)
func main() {
counts := make(map[string]int)
files := os.Args[1:]
fmt.Println(files)
if len(files) == 0 {
fmt.Println("...")
} else {
for _, arg := range files {
f, err := os.Open(arg)
if err != nil {
fmt.Fprintf(os.Stderr, "dup2: %v \n", err)
continue
}
countLines(f, counts)
f.Close()
}
}
for line, n := range counts {
if n > 1 {
fmt.Printf("%d\t%s\n", n, line)
}
}
}
func countLines(f *os.File, counts map[string]int) {
input := bufio.NewScanner(f)
for input.Scan() {
counts[input.Text()]++
}
}

27
day2/dup3.go Normal file
View File

@ -0,0 +1,27 @@
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.Println(os.Stderr, "dup3 : %v", err)
continue
}
for _, line := range strings.Split(string(data), "\n") {
counts[line]++
}
for line, n := range counts {
if n > 1 {
fmt.Printf("%d\t%s\n", n, line)
}
}
}
}

28
day2/fetch.go Normal file
View File

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

BIN
day2/lissajous Executable file

Binary file not shown.

57
day2/lissajous.go Normal file
View File

@ -0,0 +1,57 @@
package main
import (
"image"
"image/color"
"image/gif"
"io"
"math"
"math/rand"
"os"
"time"
)
var palette = []color.Color{color.Black,
color.RGBA{G: 0xff, A: 0xff}}
const (
whiteIndex = 0
blackIndex = 1
)
func main() {
rand.Seed(time.Now().UTC().UnixNano())
lissajous(os.Stdout)
}
func lissajous(out io.Writer) {
const (
cycles = 5
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), blackIndex)
}
phase += 0.1
anim.Delay = append(anim.Delay, delay)
anim.Image = append(anim.Image, img)
}
gif.EncodeAll(out, &anim)
}

BIN
day2/out.gif Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 225 KiB

BIN
day2/outGreen.gif Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 122 KiB

27
day2/practice1.7.go Normal file
View File

@ -0,0 +1,27 @@
package main
import (
"fmt"
"io"
. "net/http"
"os"
)
func main() {
for _, url := range os.Args[1:] {
resp, err := Get(url)
if err != nil {
fmt.Fprintf(os.Stderr,
"Fetch: %v", err)
os.Exit(1)
}
_, err = io.Copy(os.Stdout, resp.Body)
if err != nil {
fmt.Fprintf(os.Stderr,
"Fetch: %s as %v", url, err)
os.Exit(1)
}
resp.Body.Close()
}
}

42
day2/practice1.8.go Normal file
View File

@ -0,0 +1,42 @@
package main
import (
"fmt"
"net/http"
"os"
"strings"
)
func main() {
CheckArgsHttp(os.Args[1:]) //1.8
for _, url := range os.Args[1:] {
resp, err := http.Get(url)
if err != nil {
fmt.Fprintf(os.Stderr,
"Fetch: %v", err)
os.Exit(1)
}
// 获得http协议状态码
fmt.Printf("%s status:%s code:%d \n", url, resp.Status, resp.StatusCode)
//_, err = io.Copy(os.Stdout, resp.Body)
//if err != nil{
// fmt.Fprintf(os.Stderr,
// "Fetch: %s as %v", url,err)
// os.Exit(1)
//}
//resp.Body.Close()
}
}
func CheckArgsHttp(args []string) {
for i, _ := range args {
// 去除 空格
args[i] = strings.TrimSpace(args[i])
// 去除 /
args[i] = strings.Trim(args[i], "/")
// 添加 http前缀
if strings.HasPrefix(args[i], "http") == false {
args[i] = "http://" + args[i]
}
fmt.Println(args[i])
}
}

14
day2/test.txt Normal file
View File

@ -0,0 +1,14 @@
test 30
test 30
test 30
Hello 95
Hello 95
Hello 95
Hello 95
Linux 85
Linux 85

12
day2/test1.txt Normal file
View File

@ -0,0 +1,12 @@
test 30
test 30
test 30
Hello 95
Hello 95
Hello 95
Hello 95
Linux 85
Linux 85
Linux 85