Bu örnekte Golang'ın arabelleksiz kanalları kullanılmaktadır. Arabelleksiz kanallar her seferinde sadece bir değer içerir. Bu nedenle bunlara engelleme kanalı denir ve şu şekilde oluşturulurlar: make(chan TYPE). Kullanımı göstermek için, her 2 saniyede bir şehirleri birer birer selamlayacağız. Ancak, selamlamada 5 saniye gecikme olursa, uygulama zaman aşımına uğrar. Ayrıntılı açıklama için lütfen satır içi yorumları okuyun.


Örnek


Aşağıdaki uyku süresini 2'den 5/+ saniyeye çıkarırsanız, uygulama hiçbir şey yazdırmadan zaman aşımına uğrar. select deyiminde default blok yoktur, bu nedenle kanalımızdan bir mesaj alınana kadar engellenir, ardından engellemeye devam eder veya otomatik olarak bir zaman aşımı vakasıyla karşılaşır ve sonra çıkar. Burada main fonksiyonu alıcı, welcome fonksiyonu ise verici durumundadır.


package main

import (
"fmt"
"os"
"time"
)

func main() {
// List of cities.
cities := []string{"London", "Istanbul", "Berlin", "Madrid"}

// Create unbuffered channel.
ch := make(chan string)

// Pass channel and cities to welcome function and run it as goroutine.
go welcome(ch, cities)

// Indefinitely wait for the channel to return something.
for {
select {
// If something was received through the channel, print it.
case msg := <-ch:
fmt.Println(msg)
// If nothing was received through the channel for 5 seconds, timeout.
case <-time.After(5 * time.Second):
fmt.Println("timeout")
os.Exit(0)
}
}
}

// welcome accepts write-only channel and list of cities.
func welcome(ch chan<- string, cities []string) {
// Loop through the cities.
for _, city := range cities {
// Sleep 2 seconds before writing to the channel below.
time.Sleep(1 * time.Second)

// Write (send) greeting message to the channel.
ch <- fmt.Sprintf("Welcome to %s", city)
}
}

Çıktı


$ go run -race cmd/client/main.go
Welcome to London
Welcome to Istanbul
Welcome to Berlin
Welcome to Madrid
timeout

Örnek 2


Bu daha basit bir sürümdür çünkü zaman aşımı kullanmıyoruz. Bunun yerine, kanalı açıkça kapatıyoruz.


package main

import (
"fmt"
"time"
)

func main() {
// List of cities.
cities := []string{"London", "Istanbul", "Berlin", "Madrid"}

// Create unbuffered channel.
ch := make(chan string)

// Pass channel and cities to welcome function and run it as goroutine.
go welcome(ch, cities)

// Indefinitely wait for the channel to return something.
for msg := range ch {
fmt.Println(msg)
}

fmt.Println("done")
}

// welcome accepts write-only channel and list of cities.
func welcome(ch chan<- string, cities []string) {
// Loop through the cities.
for _, city := range cities {
// Sleep 2 seconds before writing to the channel below.
time.Sleep(1 * time.Second)

// Write (send) greeting message to the channel.
ch <- fmt.Sprintf("Welcome to %s", city)
}

// Close the channel so that range loop knows when to exit.
close(ch)
}

Welcome to London
Welcome to Istanbul
Welcome to Berlin
Welcome to Madrid
done

Örnek 3


Burada şehirleri hep birlikte değil, fonksiyona tek tek iletiyoruz.


package main

import (
"fmt"
"time"
)

func main() {
// List of cities.
cities := []string{"London", "Istanbul", "Berlin", "Madrid"}

// Create unbuffered channel.
ch := make(chan string)

for _, city := range cities {
// Pass channel and city to welcome function and run it as goroutine.
go welcome(ch, city)

// Indefinitely wait for the channel to return something.
for msg := range ch {
fmt.Println(msg)

break
}
}

fmt.Println("done")
}

// welcome accepts write-only channel and city.
func welcome(ch chan<- string, city string) {
// Sleep 2 seconds before writing to the channel below.
time.Sleep(1 * time.Second)

// Write (send) greeting message to the channel.
ch <- fmt.Sprintf("Welcome to %s", city)
}

Welcome to London
Welcome to Istanbul
Welcome to Berlin
Welcome to Madrid
done