This commit is contained in:
a.pivkin 2025-12-30 21:55:12 +03:00
parent 00b9ce122d
commit 23931220bb
8 changed files with 268 additions and 7 deletions

View File

@ -0,0 +1,3 @@
module main
go 1.25.5

View File

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

View File

@ -0,0 +1,3 @@
module main
go 1.25.5

View File

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

View File

@ -0,0 +1,34 @@
<!DOCTYPE html>
<html>
<head>
<title>TABLE</title>
<style>
body { font-family: Arial, sans-serif; margin: 40px; }
.header { background: #3498db; color: white; padding: 20px; border-radius: 8px; }
.content { margin: 20px 0; }
table { width: 100%; border-collapse: collapse; }
th, td { padding: 10px; border: 1px solid #ddd; text-align: left; }
th { background: #f4f4f4; }
.price { text-align: right; font-weight: bold; color: #27ae60; }
.total { background: #ecf0f1; font-weight: bold; }
</style>
</head>
<body>
<div class="content">
<h2>Items</h2>
<table>
<tr>
<th>Item Name</th>
<th>Price</th>
</tr>
{{range $key, $value := .Items}}
<tr>
<td>{{$key}}</td>
<td class="price">{{$value.String}}</td>
</tr>
{{end}}
</table>
</div>
</body>
</html>

View File

@ -2,10 +2,12 @@ package main
import ( import (
"bufio" "bufio"
"flag"
"fmt" "fmt"
"io" "io"
"os" "os"
"strings" "strings"
"time"
) )
type ByteCount int 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() { func main() {
flag.Parse()
fmt.Printf("Waiting %v...",period)
time.Sleep(*period)
fmt.Println()
// var bc ByteCount // var bc ByteCount
// bc.Write([]byte("Hello dodik")) // bc.Write([]byte("Hello dodik"))
// fmt.Println(bc) // fmt.Println(bc)
@ -99,14 +110,14 @@ func main() {
// writer.Write([]byte("Hello Baga")) // writer.Write([]byte("Hello Baga"))
// fmt.Printf("Bytes %d",*count) // fmt.Printf("Bytes %d",*count)
str := "hello" // str := "hello"
buf := make([]byte,100) // buf := make([]byte,100)
reader := LimitReader(strings.NewReader(str),3) // reader := LimitReader(strings.NewReader(str),3)
// // n,_ := reader.Read(buf)
// // reader := strings.NewReader(str)
// n,_ := reader.Read(buf) // n,_ := reader.Read(buf)
// reader := strings.NewReader(str) // fmt.Printf("Result is %d\n",n)
n,_ := reader.Read(buf) // fmt.Printf("String is %s",string(buf))
fmt.Printf("Result is %d\n",n)
fmt.Printf("String is %s",string(buf))
} }

View File

@ -0,0 +1,3 @@
module main
go 1.25.5

View File

@ -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(`
<table>
<tr style='text-align: left'>
<th>#</th>
<th>Title</th>
<th>Artist</th>
<th>Album</th>
</tr>
{{range .}}
<tr>
<td>{{.Title}}</td>
<td>{{.Artist}}</td>
<td>{{.Album}}</td>
</tr>
{{end}}
</table>
`))
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)
// }
}