-
-
Save greeflas/2cdc3f68b8a0dae6f5879ba8d926d4bf to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import ( | |
"fmt" | |
"time" | |
) | |
type User struct { | |
Name string | |
Ch chan string | |
} | |
type Employee struct { | |
Name string | |
Type int | |
} | |
func main() { | |
c := &User{Name: "Kate", Ch: make(chan string)} | |
arrEmployees := []*Employee{ | |
{Name: "John", Type: 1}, | |
{Name: "Liza", Type: 2}, | |
{Name: "Garry", Type: 2}, | |
} | |
go func() { | |
for _, e := range arrEmployees { | |
if e.Type == 2 { | |
c.Ch <- fmt.Sprintf("Hello, I'm %s", e.Name) | |
time.Sleep(time.Second) | |
c.Ch <- fmt.Sprintf("Waiting time is 5m") | |
break | |
} | |
} | |
close(c.Ch) | |
}() | |
for m := range c.Ch { | |
fmt.Println(m) | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
type User = { | |
name: string | |
ch: (m: string) => void | |
} | |
type Employee = { | |
name: string | |
type: number | |
} | |
const c: User = { | |
name: "Kate", | |
ch: (m: string) => console.log(m), | |
}; | |
const arrEmployees: Employee[] = [ | |
{name: "John", type: 1}, | |
{name: "Liza", type: 2}, | |
{name: "Garry", type: 2}, | |
] | |
for (let e of arrEmployees) { | |
if (e.type == 2) { | |
c.ch(`Hello, I'm ${e.name}`) | |
setTimeout(() => c.ch("Waiting time is 5m"), 1000) | |
break | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import ( | |
"fmt" | |
"time" | |
) | |
type DriverRank int | |
const ( | |
DriverRankStandard DriverRank = 1 | |
DriverRankPremium DriverRank = 2 | |
) | |
type Driver struct { | |
FirstName string | |
Rank DriverRank | |
} | |
type Passenger struct { | |
FirstName string | |
NotificationChannel chan string | |
} | |
func main() { | |
passenger := &Passenger{FirstName: "Kate", NotificationChannel: make(chan string)} | |
go callTaxi(passenger) | |
notifyPassengerOnUpdates(passenger) | |
} | |
func callTaxi(passenger *Passenger) { | |
driver := findDriver(DriverRankPremium) | |
passenger.NotificationChannel <- fmt.Sprintf("Hello, I'm %s", driver.FirstName) | |
time.Sleep(time.Second) | |
passenger.NotificationChannel <- fmt.Sprintf("Waiting time is 5m") | |
close(passenger.NotificationChannel) | |
} | |
func notifyPassengerOnUpdates(passenger *Passenger) { | |
for update := range passenger.NotificationChannel { | |
fmt.Println(update) | |
} | |
} | |
func findDriver(rank DriverRank) *Driver { | |
availableDrivers := []*Driver{ | |
{FirstName: "John", Rank: DriverRankStandard}, | |
{FirstName: "Liza", Rank: DriverRankPremium}, | |
{FirstName: "Garry", Rank: DriverRankPremium}, | |
} | |
for _, driver := range availableDrivers { | |
if driver.Rank == rank { | |
return driver | |
} | |
} | |
return nil | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
type channel = (update: string) => void | |
type Passenger = { | |
firstName: string | |
notificationChannel: channel | |
rank: Rank | |
} | |
enum Rank { | |
STANDARD = 1, | |
PREMIUM = 2, | |
} | |
type Driver = { | |
firstName: string | |
rank: Rank | |
} | |
const callTaxi = (passenger: Passenger) => { | |
const driver = findDriver(passenger.rank) | |
passenger.notificationChannel(`Hello, I'm ${driver.firstName}`) | |
setTimeout(() => passenger.notificationChannel("Waiting time is 5m"), 1000) | |
} | |
const findDriver = (rank: Rank): Driver => { | |
const availableDrivers: Driver[] = [ | |
{firstName: "John", rank: Rank.STANDARD}, | |
{firstName: "Liza", rank: Rank.PREMIUM}, | |
{firstName: "Garry", rank: Rank.PREMIUM}, | |
] | |
return availableDrivers.find((driver: Driver) => driver.rank === rank) | |
} | |
const passengerKate: Passenger = { | |
firstName: "Kate", | |
notificationChannel: (update: string) => console.log(update), | |
rank: Rank.PREMIUM | |
}; | |
callTaxi(passengerKate) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment