master
dugulp 2022-10-22 01:21:54 +08:00
commit 51829d8ada
11 changed files with 177 additions and 0 deletions

8
.idea/.gitignore vendored Normal file
View File

@ -0,0 +1,8 @@
# 默认忽略的文件
/shelf/
/workspace.xml
# 基于编辑器的 HTTP 客户端请求
/httpRequests/
# Datasource local storage ignored files
/dataSources/
/dataSources.local.xml

9
.idea/Study.iml Normal file
View File

@ -0,0 +1,9 @@
<?xml version="1.0" encoding="UTF-8"?>
<module type="WEB_MODULE" version="4">
<component name="Go" enabled="true" />
<component name="NewModuleRootManager">
<content url="file://$MODULE_DIR$" />
<orderEntry type="inheritedJdk" />
<orderEntry type="sourceFolder" forTests="false" />
</component>
</module>

8
.idea/modules.xml Normal file
View File

@ -0,0 +1,8 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="ProjectModuleManager">
<modules>
<module fileurl="file://$PROJECT_DIR$/.idea/Study.iml" filepath="$PROJECT_DIR$/.idea/Study.iml" />
</modules>
</component>
</project>

6
.idea/vcs.xml Normal file
View File

@ -0,0 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="VcsDirectoryMappings">
<mapping directory="$PROJECT_DIR$" vcs="Git" />
</component>
</project>

52
day3/fetchall.go Normal file
View File

@ -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]
}
}
}

7
day3/hello.go Normal file
View File

@ -0,0 +1,7 @@
package main
import "fmt"
func main() {
fmt.Println("hello world!")
}

17
day3/server1.go Normal file
View File

@ -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)
}

29
day3/server2.go Normal file
View File

@ -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()
}

18
day4/boiling.go Normal file
View File

@ -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
}

20
day4/echo4.go Normal file
View File

@ -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()
}
}

3
go.mod Normal file
View File

@ -0,0 +1,3 @@
module Study
go 1.19