From 51829d8ada124b3375221f235940b6012a7cbf12 Mon Sep 17 00:00:00 2001 From: dugulp Date: Sat, 22 Oct 2022 01:21:54 +0800 Subject: [PATCH] first --- .idea/.gitignore | 8 ++++++++ .idea/Study.iml | 9 ++++++++ .idea/modules.xml | 8 ++++++++ .idea/vcs.xml | 6 ++++++ day3/fetchall.go | 52 +++++++++++++++++++++++++++++++++++++++++++++++ day3/hello.go | 7 +++++++ day3/server1.go | 17 ++++++++++++++++ day3/server2.go | 29 ++++++++++++++++++++++++++ day4/boiling.go | 18 ++++++++++++++++ day4/echo4.go | 20 ++++++++++++++++++ go.mod | 3 +++ 11 files changed, 177 insertions(+) create mode 100644 .idea/.gitignore create mode 100644 .idea/Study.iml create mode 100644 .idea/modules.xml create mode 100644 .idea/vcs.xml create mode 100644 day3/fetchall.go create mode 100644 day3/hello.go create mode 100644 day3/server1.go create mode 100644 day3/server2.go create mode 100644 day4/boiling.go create mode 100644 day4/echo4.go create mode 100644 go.mod diff --git a/.idea/.gitignore b/.idea/.gitignore new file mode 100644 index 0000000..35410ca --- /dev/null +++ b/.idea/.gitignore @@ -0,0 +1,8 @@ +# 默认忽略的文件 +/shelf/ +/workspace.xml +# 基于编辑器的 HTTP 客户端请求 +/httpRequests/ +# Datasource local storage ignored files +/dataSources/ +/dataSources.local.xml diff --git a/.idea/Study.iml b/.idea/Study.iml new file mode 100644 index 0000000..5e764c4 --- /dev/null +++ b/.idea/Study.iml @@ -0,0 +1,9 @@ + + + + + + + + + \ No newline at end of file diff --git a/.idea/modules.xml b/.idea/modules.xml new file mode 100644 index 0000000..4f5ac2f --- /dev/null +++ b/.idea/modules.xml @@ -0,0 +1,8 @@ + + + + + + + + \ No newline at end of file diff --git a/.idea/vcs.xml b/.idea/vcs.xml new file mode 100644 index 0000000..94a25f7 --- /dev/null +++ b/.idea/vcs.xml @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/day3/fetchall.go b/day3/fetchall.go new file mode 100644 index 0000000..75334c6 --- /dev/null +++ b/day3/fetchall.go @@ -0,0 +1,52 @@ +package main + +import ( + "fmt" + "io" + "net/http" + "os" + "strings" + "time" +) + +func main() { + CheckHttpArgs(os.Args[1:]) + start := time.Now() + ch := make(chan string) + for _, url := range os.Args[1:] { + go fetch(url, ch) + } + for range os.Args[1:] { + fmt.Println(<-ch) + } + fmt.Printf("%.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.Sprintln(err) + return + } + nbyte, err := io.Copy(io.Discard, resp.Body) + if err != nil { + ch <- fmt.Sprintf("while reading %s : %v", url, err) + return + } + secs := time.Since(start).Seconds() + ch <- fmt.Sprintf("%.2fs %7d %s", secs, nbyte, url) +} + +func CheckHttpArgs(args []string) { + for i, _ := range args { + // 去除空格 + strings.TrimSpace(args[i]) + // 去除 / + strings.Trim(args[i], "/") + + if strings.HasPrefix(args[i], "http") != true { + args[i] = "http://" + args[i] + } + } +} diff --git a/day3/hello.go b/day3/hello.go new file mode 100644 index 0000000..7776ec8 --- /dev/null +++ b/day3/hello.go @@ -0,0 +1,7 @@ +package main + +import "fmt" + +func main() { + fmt.Println("hello world!") +} diff --git a/day3/server1.go b/day3/server1.go new file mode 100644 index 0000000..c6fa261 --- /dev/null +++ b/day3/server1.go @@ -0,0 +1,17 @@ +package main + +import ( + "fmt" + "log" + "net/http" +) + +func main() { + http.HandleFunc("/", handler) + log.Fatal(http.ListenAndServe("localhost:8090", nil)) +} + +func handler(w http.ResponseWriter, r *http.Request) { + fmt.Fprintf(w, "url.Path = %q \n", r.URL.Path) + +} diff --git a/day3/server2.go b/day3/server2.go new file mode 100644 index 0000000..e55de1d --- /dev/null +++ b/day3/server2.go @@ -0,0 +1,29 @@ +package main + +import ( + "fmt" + "log" + "net/http" + "sync" +) + +var mu sync.Mutex +var count int + +func main() { + http.HandleFunc("/", handler) + http.HandleFunc("/count", counter) + log.Fatal(http.ListenAndServe("localhost:8090", nil)) +} + +func handler(w http.ResponseWriter, r *http.Request) { + mu.Lock() + count++ + mu.Unlock() + fmt.Fprintf(w, "URL.Path = %q\n", r.URL.Path) +} +func counter(w http.ResponseWriter, r *http.Request) { + mu.Lock() + fmt.Fprintf(w, "count: %d", count) + mu.Unlock() +} diff --git a/day4/boiling.go b/day4/boiling.go new file mode 100644 index 0000000..a43e1ea --- /dev/null +++ b/day4/boiling.go @@ -0,0 +1,18 @@ +package main + +import "fmt" + +const boilinF = 212.0 // 常量 + +func main() { + f := boilinF + c := (f - 32) * 5 / 9 + fmt.Printf("Boiling point = %g°F or %g°C\n", + f, c) + fmt.Printf("%gF = %gC", 32.0, fToC(32.0)) +} + +// fToC +func fToC(f float64) float64 { + return (f - 32) * 5 / 9 +} diff --git a/day4/echo4.go b/day4/echo4.go new file mode 100644 index 0000000..b9c5284 --- /dev/null +++ b/day4/echo4.go @@ -0,0 +1,20 @@ +package main + +import ( + "flag" + "fmt" + "strings" +) + +var n = flag.Bool("n", false, + "omit trailing newline") +var sep = flag.String("s", "", + "separtor") + +func main() { + flag.Parse() + fmt.Print(strings.Join(flag.Args(), *sep)) + if !*n { + fmt.Println() + } +} diff --git a/go.mod b/go.mod new file mode 100644 index 0000000..6666431 --- /dev/null +++ b/go.mod @@ -0,0 +1,3 @@ +module Study + +go 1.19