Advertisement
fenixD3

avito platform

Aug 7th, 2024
325
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Go 2.63 KB | None | 0 0
  1. //----------------------------------------------
  2. package main
  3.  
  4. import "fmt"
  5.  
  6. type Person struct {
  7.     Name string
  8. }
  9.  
  10. func changeName(person *Person) {
  11.     person.Name = "Alice"
  12. }
  13.  
  14. func main() {
  15.     person := &Person{
  16.         Name: "Bob",
  17.     }
  18.     fmt.Println(person.Name) // Bob
  19.     changeName(person)
  20.     fmt.Println(person.Name) // Alice
  21. }
  22.  
  23.  
  24.  
  25. //----------------------------------------------
  26. // Мы делаем сервис, который собирает сниппет товара. Каждый сниппет состоит из отформатированного описания и стоимости товара в рублях.
  27. // Чтобы собрать сниппет нужно:
  28. // Получить описание из одного сервиса, а затем отформатированное его через prettify
  29. // Получить цену (в долларах) из другого сервиса, а затем перевести ее в рубли через priceToRub
  30. // Вернуть готовый сниппет
  31.  
  32.  
  33. package main
  34.  
  35.  
  36. // itemDescription симулирует получение описания из сервиса
  37. func itemDescription(itemID int) string {
  38.    time.Sleep(time.Second * 1)
  39.    return "☆☆☆Lorem ♡♡♡ ipsum  dolor sit amet..."
  40. }
  41.  
  42.  
  43. // itemPrice симулирует получение цены в долларах из сервиса
  44. func itemPrice(itemId int) float64 {
  45.    time.Sleep(2 * time.Second)
  46.    return 100
  47. }
  48.  
  49.  
  50. func prettify(description string) (string, error) {
  51.    time.Sleep(time.Second * 1)
  52.    re := regexp.MustCompile("\\W+")
  53.    return re.ReplaceAllString(description, "")
  54. }
  55.  
  56.  
  57. func priceToRub(price float64) (float64, error) {
  58.    time.Sleep(1 * time.Second)
  59.    return price * 70
  60. }
  61.  
  62.  
  63. type Snippet struct {
  64.    Price       float64
  65.    Description string
  66. }
  67.  
  68.  
  69. func BuildSnippet(itemId int) (Snippet, error) {
  70.    // write your code here
  71.    type descResult struct {
  72.        desc string
  73.        err error
  74.    }
  75.  
  76.    descCh := make(chan descResult, 1)
  77.    go func() {
  78.         rawDesc := itemDescription(itemId)
  79.         finalDesc, err := prettify(rawDesc)
  80.  
  81.         var toSend descResult = descResult{finalDesc, err}
  82.         descCh <- toSend
  83.         close(descCh)
  84.    }()
  85.  
  86.    rawPrice := itemPrice(itemId)
  87.    finalPrice, err := priceToRub(rawPrice)
  88.    if err != nil {
  89.        return
  90.     }
  91.  
  92.    descItem := <-descCh
  93.  
  94.    if err != nil || descItem.err != nil {
  95.        return Snippet{}, fmt.Errorf("Error form description %v. From price %v", descItem.err, err)
  96.    }
  97.    return Snippet{Price: finalPrice, Description: desc}, nil
  98. }
  99.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement