Welcome to the Anti-Spam Bot project! This bot is designed to monitor and manage spam activities, ensuring smooth and uninterrupted conversations.
https://github.com/evgensr/anti-spam
Welcome to the Anti-Spam Bot project! This bot is designed to monitor and manage spam activities, ensuring smooth and uninterrupted conversations.
https://github.com/evgensr/anti-spam
Fixing For Loops in Go 1.22
https://go.dev/blog/loopvar-preview
David Chase и Russ Cox в официальном блоге Go рассказывают про проблему LoopVar (переменной цикла) и приводят конкретные примеры, в том числе из реального кейса проекта — Lets Encrypt.
Затем рассказывают, как именно эта проблема решается. Если кратко:
> change for loops to make these variables have per-iteration scope instead of per-loop scope
Как вы помните, эта фича уже есть в Go v1.21, но её нужно включить:
GOEXPERIMENT=loopvar go test
А в версии 1.22 она уже будет работать по-умолчанию
func forex() string { client := &http.Client{ Timeout: 10 * time.Second, } req, err := http.NewRequest("GET", "https://www.investing.com/currencies/usd-rub", nil) if err != nil { log.Fatal(err) } req.Header.Set("User-Agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.110 Safari/537") res, err := client.Do(req) if err != nil { log.Println(err) } defer res.Body.Close() if res.StatusCode != 200 { log.Printf("status code error: %d %s", res.StatusCode, res.Status) } // Load the HTML document doc, err := goquery.NewDocumentFromReader(res.Body) if err != nil { log.Println(err) } var price string doc.Find("span[data-test='instrument-price-last']").Each(func(index int, item *goquery.Selection) { price = item.Text() fmt.Println(price) }) return price }
package main import ( "fmt" "strings" ) func main() { fmt.Println( // true strings.Contains("test", "es"), // 2 strings.Count("test", "t"), // true strings.HasPrefix("test", "te"), // true strings.HasSuffix("test", "st"), // 1 strings.Index("test", "e"), // "a-b" strings.Join([]string{"a","b"}, "-"), // == "aaaaa" strings.Repeat("a", 5), // "bbaa" strings.Replace("aaaa", "a", "b", 2), // []string{"a","b","c","d","e"} strings.Split("a-b-c-d-e", "-"), // "test" strings.ToLower("TEST"), // "TEST" strings.ToUpper("test"), ) }
Проверка, что наша структура user реализует интерфейс User
Создаем переменную которая не нужна с типом User и значением структуры user
var _ User = &user{} type User interface { ChangFIO(newFio string) } type user struct { FIO string } func (u *user) ChangFIO(newFio string) { }
package main import ( "fmt" "reflect" ) var funcMap = map[string]interface{}{ "Hello": Hello, } func Hello(name string) { fmt.Printf("Hello, %s!\n", name) } func main() { nameFunc := "Hello" if fn, ok := funcMap[nameFunc]; ok { funcValue := reflect.ValueOf(fn) args := []reflect.Value{reflect.ValueOf("Gopher")} funcValue.Call(args) } else { fmt.Println("Function not found!") } }
// listen to OS signals and gracefully shutdown HTTP server stopped := make(chan struct{}) go func() { sigint := make(chan os.Signal, 1) signal.Notify(sigint, os.Interrupt, syscall.SIGINT, syscall.SIGTERM) <-sigint ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second) defer cancel() if err := srv.Shutdown(ctx); err != nil { log.Printf("HTTP Server Shutdown Error: %v", err) } close(stopped) }() log.Printf("Starting HTTP server on %s", cfg.HTTPAddr) // start HTTP server if err := srv.ListenAndServe(); err != http.ErrServerClosed { log.Fatalf("HTTP server ListenAndServe Error: %v", err) } <-stopped
package main import ( "embed" "html/template" "log" "net/http" ) var ( //go:embed resources res embed.FS pages = map[string]string{ "/": "resources/index.gohtml", } ) func main() { http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) { page, ok := pages[r.URL.Path] if !ok { w.WriteHeader(http.StatusNotFound) return } tpl, err := template.ParseFS(res, page) if err != nil { log.Printf("page %s not found in pages cache...", r.RequestURI) w.WriteHeader(http.StatusInternalServerError) return } w.Header().Set("Content-Type", "text/html") w.WriteHeader(http.StatusOK) data := map[string]interface{}{ "userAgent": r.UserAgent(), } if err := tpl.Execute(w, data); err != nil { return } }) http.FileServer(http.FS(res)) log.Println("server started...") err := http.ListenAndServe(":8088", nil) if err != nil { panic(err) } }
index.gohtml
<html lang="en"> <head> <meta charset="UTF-8"/> <title>go:embed demo</title> </head> <body> <div> <h1>Hello, {{ .userAgent }}!</h1> <p>If you see this, then go:embed worked!</p> </div> </body> </html>
The standard library packages exist in the first group.
Then Third — party libs.
At the end of the Imports from your project.