Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //----------------------------------------------
- package main
- import "fmt"
- type Person struct {
- Name string
- }
- func changeName(person *Person) {
- person.Name = "Alice"
- }
- func main() {
- person := &Person{
- Name: "Bob",
- }
- fmt.Println(person.Name) // Bob
- changeName(person)
- fmt.Println(person.Name) // Alice
- }
- //----------------------------------------------
- // Мы делаем сервис, который собирает сниппет товара. Каждый сниппет состоит из отформатированного описания и стоимости товара в рублях.
- // Чтобы собрать сниппет нужно:
- // Получить описание из одного сервиса, а затем отформатированное его через prettify
- // Получить цену (в долларах) из другого сервиса, а затем перевести ее в рубли через priceToRub
- // Вернуть готовый сниппет
- package main
- // 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, error) {
- time.Sleep(time.Second * 1)
- re := regexp.MustCompile("\\W+")
- return re.ReplaceAllString(description, "")
- }
- func priceToRub(price float64) (float64, error) {
- time.Sleep(1 * time.Second)
- return price * 70
- }
- type Snippet struct {
- Price float64
- Description string
- }
- func BuildSnippet(itemId int) (Snippet, error) {
- // write your code here
- type descResult struct {
- desc string
- err error
- }
- descCh := make(chan descResult, 1)
- go func() {
- rawDesc := itemDescription(itemId)
- finalDesc, err := prettify(rawDesc)
- var toSend descResult = descResult{finalDesc, err}
- descCh <- toSend
- close(descCh)
- }()
- rawPrice := itemPrice(itemId)
- finalPrice, err := priceToRub(rawPrice)
- if err != nil {
- return
- }
- descItem := <-descCh
- if err != nil || descItem.err != nil {
- return Snippet{}, fmt.Errorf("Error form description %v. From price %v", descItem.err, err)
- }
- return Snippet{Price: finalPrice, Description: desc}, nil
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement