Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package build_cnippet_avito
- import (
- "fmt"
- "regexp"
- "time"
- )
- // itemDescription симулирует получение описания из сервиса
- func itemDescription(itemID int) string {
- time.Sleep(time.Second * 1)
- return "☆☆☆Lorem ♡♡♡ ipsum dolor sit amet..."
- }
- // itemPrice симулирует получение цены в долларах из сервиса
- func itemPrice(itemId int) float64 {
- time.Sleep(2 * time.Second)
- return 100
- }
- func prettify(description string) string {
- time.Sleep(time.Second * 1)
- re := regexp.MustCompile("\\W+")
- panic("Huy")
- return re.ReplaceAllString(description, "")
- }
- func priceToRub(price float64) float64 {
- time.Sleep(1 * time.Second)
- return price * 70
- }
- type Snippet struct {
- Price float64
- Description string
- }
- func BuildSnippet(itemId int) Snippet {
- // write your code here
- //type descResult struct {
- // desc string
- // err error
- //}
- descCh := make(chan string)
- go func() {
- defer func() {
- fmt.Println("recovered:", recover())
- }()
- rawDesc := itemDescription(itemId)
- finalDesc := prettify(rawDesc)
- fmt.Println("Here")
- descCh <- finalDesc
- close(descCh)
- }()
- rawPrice := itemPrice(itemId)
- finalPrice := priceToRub(rawPrice)
- descItem := <-descCh
- return Snippet{Price: finalPrice, Description: descItem}
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement