From 23931220bbdceda40aed0b98a16a8e53cbe1c946 Mon Sep 17 00:00:00 2001 From: "a.pivkin" Date: Tue, 30 Dec 2025 21:55:12 +0300 Subject: [PATCH] fgdfg --- book_with_bridge/flag/go.mod | 3 + book_with_bridge/flag/main.go | 31 ++++++++++ book_with_bridge/http/go.mod | 3 + book_with_bridge/http/main.go | 96 +++++++++++++++++++++++++++++ book_with_bridge/http/template.html | 34 ++++++++++ book_with_bridge/interfaces/main.go | 25 +++++--- book_with_bridge/tabwriter/go.mod | 3 + book_with_bridge/tabwriter/main.go | 80 ++++++++++++++++++++++++ 8 files changed, 268 insertions(+), 7 deletions(-) create mode 100644 book_with_bridge/flag/go.mod create mode 100644 book_with_bridge/flag/main.go create mode 100644 book_with_bridge/http/go.mod create mode 100644 book_with_bridge/http/main.go create mode 100644 book_with_bridge/http/template.html create mode 100644 book_with_bridge/tabwriter/go.mod create mode 100644 book_with_bridge/tabwriter/main.go diff --git a/book_with_bridge/flag/go.mod b/book_with_bridge/flag/go.mod new file mode 100644 index 0000000..371eb8f --- /dev/null +++ b/book_with_bridge/flag/go.mod @@ -0,0 +1,3 @@ +module main + +go 1.25.5 diff --git a/book_with_bridge/flag/main.go b/book_with_bridge/flag/main.go new file mode 100644 index 0000000..bf23d52 --- /dev/null +++ b/book_with_bridge/flag/main.go @@ -0,0 +1,31 @@ +package main + +import "fmt" + +type Kek interface { + lol() + azaza() +} + +type Test struct { + x,y string +} + +func(t Test) lol() { + fmt.Printf("I am lol method - %s\n",t.x) +} + +func(t Test) azaza() { + fmt.Printf("I am azaza method %s\n",t.y) +} + +var _ Kek = (&Test{}) + +func main() { + t := Test{ + x: "one", + y: "two", + } + t.lol() + t.azaza() +} diff --git a/book_with_bridge/http/go.mod b/book_with_bridge/http/go.mod new file mode 100644 index 0000000..371eb8f --- /dev/null +++ b/book_with_bridge/http/go.mod @@ -0,0 +1,3 @@ +module main + +go 1.25.5 diff --git a/book_with_bridge/http/main.go b/book_with_bridge/http/main.go new file mode 100644 index 0000000..4cd06ae --- /dev/null +++ b/book_with_bridge/http/main.go @@ -0,0 +1,96 @@ +package main + +import ( + "fmt" + "log" + "net/http" + "strconv" + "html/template" +) + +type rub float64 + +func (r rub) String() string { + return fmt.Sprintf("%.2f",r) +} + +var tmpl *template.Template + + +func Render(w http.ResponseWriter, req *http.Request) { + err := tmpl.Execute(w,req) + if err != nil { + http.Error(w,err.Error(),http.StatusInternalServerError) + } +} + +type database map[string]rub + +type List struct { + Items database +} + +func (db *database) list(w http.ResponseWriter, req *http.Request) { + data := List{ + Items: *db, + } + + err := tmpl.ExecuteTemplate(w,"template.html",data) + if err != nil { + http.Error(w,err.Error(),http.StatusInternalServerError) + } +} + +func (db *database) price(w http.ResponseWriter,req *http.Request) { + item := req.URL.Query().Get("item") + v,ok := (*db)[item] + if !ok { + w.WriteHeader(http.StatusNotFound) + fmt.Fprintf(w,"%s not found in goods list",item) + return + } + fmt.Fprintf(w,"Price of %s is %s",item,v) +} + +func (db *database) update(w http.ResponseWriter, req *http.Request) { + item := req.URL.Query().Get("item") + price := req.URL.Query().Get("price") + f,err := strconv.ParseFloat(price,2) + if err != nil { + log.Fatal(err) + } + (*db)[item] = rub(f) + fmt.Fprintf(w,"item %s and price %s",item,(*db)[item]) +} + +// func (db database) ServeHTTP(w http.ResponseWriter, r *http.Request ) { +// switch r.URL.Path { +// case "/list": +// for k,v := range db { +// fmt.Fprintf(w,"%s costs %s rub\n",k,v) +// } +// case "/price": +// item := r.URL.Query().Get("item") +// v,ok := db[item] +// if !ok { +// w.WriteHeader(http.StatusNotFound) +// fmt.Fprintf(w,"%s not found in goods list",item) +// return +// } +// fmt.Fprintf(w,"Price of %s is %s",item,v) +// } +// } + +func main() { + db := database{ + "sneakers": 10.0, + "boots": 20, + } + // mux.Handle("/list",http.HandlerFunc(db.list)) + // mux.Handle("/price",http.HandlerFunc(db.price)) + tmpl = template.Must(template.ParseFiles("template.html")) + http.HandleFunc("/list",db.list) + http.HandleFunc("/price",db.price) + http.HandleFunc("/update",db.update) + log.Fatal(http.ListenAndServe(":9090",nil)) +} diff --git a/book_with_bridge/http/template.html b/book_with_bridge/http/template.html new file mode 100644 index 0000000..841a68b --- /dev/null +++ b/book_with_bridge/http/template.html @@ -0,0 +1,34 @@ + + + + TABLE + + + + +
+

Items

+ + + + + + {{range $key, $value := .Items}} + + + + + {{end}} +
Item NamePrice
{{$key}}{{$value.String}}
+
+ + \ No newline at end of file diff --git a/book_with_bridge/interfaces/main.go b/book_with_bridge/interfaces/main.go index fb5317c..bbd9566 100644 --- a/book_with_bridge/interfaces/main.go +++ b/book_with_bridge/interfaces/main.go @@ -2,10 +2,12 @@ package main import ( "bufio" + "flag" "fmt" "io" "os" "strings" + "time" ) type ByteCount int @@ -86,7 +88,16 @@ func LimitReader(r io.Reader,n int) io.Reader { } } +// Проверка, что ByteCount действительно соответствует интерфейсу io.Writer +var _ io.Writer = new(ByteCount) + +var period = flag.Duration("period",1 * time.Second,"sleep period") + func main() { + flag.Parse() + fmt.Printf("Waiting %v...",period) + time.Sleep(*period) + fmt.Println() // var bc ByteCount // bc.Write([]byte("Hello dodik")) // fmt.Println(bc) @@ -99,14 +110,14 @@ func main() { // writer.Write([]byte("Hello Baga")) // fmt.Printf("Bytes %d",*count) - str := "hello" - buf := make([]byte,100) - reader := LimitReader(strings.NewReader(str),3) + // str := "hello" + // buf := make([]byte,100) + // reader := LimitReader(strings.NewReader(str),3) + // // n,_ := reader.Read(buf) + // // reader := strings.NewReader(str) // n,_ := reader.Read(buf) - // reader := strings.NewReader(str) - n,_ := reader.Read(buf) - fmt.Printf("Result is %d\n",n) - fmt.Printf("String is %s",string(buf)) + // fmt.Printf("Result is %d\n",n) + // fmt.Printf("String is %s",string(buf)) } diff --git a/book_with_bridge/tabwriter/go.mod b/book_with_bridge/tabwriter/go.mod new file mode 100644 index 0000000..371eb8f --- /dev/null +++ b/book_with_bridge/tabwriter/go.mod @@ -0,0 +1,3 @@ +module main + +go 1.25.5 diff --git a/book_with_bridge/tabwriter/main.go b/book_with_bridge/tabwriter/main.go new file mode 100644 index 0000000..158b3c6 --- /dev/null +++ b/book_with_bridge/tabwriter/main.go @@ -0,0 +1,80 @@ +package main + +import ( + "fmt" + "html/template" +// "log" + "os" + "sort" + "text/tabwriter" + "time" +) + +type Track struct { + Title string + Artist string + Album string + Year int + Length time.Duration +} + +var tracks = []*Track{ + {"Go", "Delilah", "From the Roots Up", 2012, length("3m38s")}, + {"Go", "Moby", "Moby", 1992, length("3m37s")}, + {"Go Ahead", "Alicia Keys", "As I Am", 2007, length("4m36s")}, + {"Ready 2 Go", "Martin Solveig", "Smash", 2011, length("4m24s")}, +} + +var issueList = template.Must(template.New("tracklist").Parse(` + + + + + + + +{{range .}} + + + + + +{{end}} +
#TitleArtistAlbum
{{.Title}}{{.Artist}}{{.Album}}
+`)) + + +func length(s string) time.Duration { + d, err := time.ParseDuration(s) + if err != nil { + panic(s) + } + return d +} + +func PrintTracks(tracks []*Track) { + const format = "%v\t%v\t%v\t%v\t%v\n" + tw := tabwriter.NewWriter(os.Stdout,0,8,2,' ',0) + fmt.Fprintf(tw,format,"Title","Artist","Album","Year","Length") + fmt.Fprintf(tw, format, "-----", "------", "-----", "----", "------") + for _, t := range tracks { + fmt.Fprintf(tw,format,t.Title,t.Artist,t.Album,t.Year,t.Length) + } + tw.Flush() +} + +type byArtists []*Track + +func (x byArtists) Len() int {return len(x)} +func (x byArtists) Less(i,j int) bool {return x[i].Artist < x[j].Artist} +func (x byArtists) Swap(i,j int) { x[i],x[j] = x[j],x[i]} + +func main() { + PrintTracks(tracks) + fmt.Println() + sort.Sort(byArtists(tracks)) + PrintTracks(tracks) + // if err := issueList.Execute(os.Stdout,tracks); err != nil { + // log.Fatal(err) + // } +} \ No newline at end of file