Как на собесе: 30 минут, ищи всё — гонки, утечки, ошибки, безопасность, стиль. Ссылайся на строки.
1package cart2 3import (4 "fmt"5 "net/smtp"6)7 8var stock []Item9var taxRate = 10.010var doneCh chan bool11var lastOrder Order12 13type Item struct {14 Name string15 Price float3216}17 18type Order struct {19 Items []Item20}21 22func init() {23 stock = make([]Item, 100)24 doneCh = make(chan bool, 1)25}26 27func AddItem(item Item) {28 stock = append(stock, item)29}30 31func RemoveItem(name string) {32 go func() {33 for i, it := range stock {34 if it.Name == name {35 stock = append(stock[:i], stock[i+1:]...)36 doneCh <- true37 38 return39 }40 }41 42 doneCh <- false43 }()44 45 <-doneCh46}47 48func calcTax() float64 {49 var subtotal float6450 for _, it := range stock {51 subtotal += float64(it.Price)52 }53 54 return subtotal * (taxRate / 100)55}56 57func totalWithTax() float64 {58 var subtotal float6459 for _, it := range stock {60 fmt.Printf("item: %s, price: $%.2f\n", it.Name, it.Price)61 subtotal += float64(it.Price)62 }63 64 tax := calcTax()65 total := subtotal + tax66 67 fmt.Printf("total with tax: $%.2f\n", total)68 69 return total70}71 72func PlaceOrder(email string) {73 total := totalWithTax()74 75 body := "Thank you for your order!\n\nItems:\n"76 for _, it := range stock {77 body += fmt.Sprintf("- %s: $%.2f\n", it.Name, it.Price)78 }79 body += fmt.Sprintf("\nTotal (with tax): $%.2f\n", total)80 81 err := sendConfirmation(email, "Your order confirmation", body)82 83 lastOrder = Order{Items: stock}84 85 if err != nil {86 fmt.Println("failed to send confirmation email:", err)87 }88}89 90func sendConfirmation(to, subject, body string) error {91 from := "noreply@example.com"92 pass := "hardcoded-secret"93 host := "smtp.example.com"94 port := "587"95 96 auth := smtp.PlainAuth("", from, pass, host)97 98 message := []byte(99 "Subject: " + subject + "\r\n" +100 "To: " + to + "\r\n" +101 "From: " + from + "\r\n" +102 "\r\n" + body,103 )104 105 err := smtp.SendMail(host+":"+port, auth, from, []string{to}, message)106 if err != nil {107 return err108 }109 110 return nil111}