{"id":463824,"date":"2025-06-19T09:00:11","date_gmt":"2025-06-19T09:00:11","guid":{"rendered":"http:\/\/savepearlharbor.com\/?p=463824"},"modified":"-0001-11-30T00:00:00","modified_gmt":"2025-06-19T09:00:11","slug":"","status":"publish","type":"post","link":"https:\/\/savepearlharbor.com\/?p=463824","title":{"rendered":"<span>7 \u0448\u0430\u0431\u043b\u043e\u043d\u043e\u0432 \u043a\u043e\u043d\u043a\u0443\u0440\u0435\u043d\u0442\u043d\u043e\u0441\u0442\u0438 \u0432 Go, \u043a\u043e\u0442\u043e\u0440\u044b\u0435 \u0432\u0430\u043c \u0441\u0442\u043e\u0438\u0442 \u0437\u043d\u0430\u0442\u044c<\/span>"},"content":{"rendered":"<div><!--[--><!--]--><\/div>\n<div id=\"post-content-body\">\n<div>\n<div class=\"article-formatted-body article-formatted-body article-formatted-body_version-2\">\n<div xmlns=\"http:\/\/www.w3.org\/1999\/xhtml\">\n<p>\u041a\u043e\u043d\u043a\u0443\u0440\u0435\u043d\u0442\u043d\u043e\u0441\u0442\u044c \u2014 \u043e\u0434\u043d\u0430 \u0438\u0437 \u0441\u0430\u043c\u044b\u0445 \u043c\u043e\u0449\u043d\u044b\u0445 \u0432\u043e\u0437\u043c\u043e\u0436\u043d\u043e\u0441\u0442\u0435\u0439 Go, \u0438 \u0435\u0451 \u043e\u0441\u0432\u043e\u0435\u043d\u0438\u0435 \u043a\u0440\u0438\u0442\u0438\u0447\u0435\u0441\u043a\u0438 \u0432\u0430\u0436\u043d\u043e \u0434\u043b\u044f \u0441\u043e\u0437\u0434\u0430\u043d\u0438\u044f \u043c\u0430\u0441\u0448\u0442\u0430\u0431\u0438\u0440\u0443\u0435\u043c\u044b\u0445 \u0438 \u044d\u0444\u0444\u0435\u043a\u0442\u0438\u0432\u043d\u044b\u0445 \u043f\u0440\u0438\u043b\u043e\u0436\u0435\u043d\u0438\u0439. \u0412 \u044d\u0442\u043e\u0439 \u0441\u0442\u0430\u0442\u044c\u0435 \u043c\u044b \u0440\u0430\u0441\u0441\u043c\u043e\u0442\u0440\u0438\u043c 7 \u0448\u0430\u0431\u043b\u043e\u043d\u043e\u0432 \u043a\u043e\u043d\u043a\u0443\u0440\u0435\u043d\u0442\u043d\u043e\u0441\u0442\u0438 \u0432 Go, \u043a\u043e\u0442\u043e\u0440\u044b\u0435 \u043f\u043e\u043c\u043e\u0433\u0443\u0442 \u0432\u0430\u043c \u043f\u0438\u0441\u0430\u0442\u044c \u043d\u0430\u0434\u0451\u0436\u043d\u044b\u0439 \u043a\u043e\u0434.<\/p>\n<h3>1. \u041f\u0443\u043b \u0432\u043e\u0440\u043a\u0435\u0440\u043e\u0432<\/h3>\n<p><strong>\u041e\u043f\u0438\u0441\u0430\u043d\u0438\u0435<\/strong>: \u041f\u0443\u043b \u0432\u043e\u0440\u043a\u0435\u0440\u043e\u0432 \u0441\u043e\u0437\u0434\u0430\u0451\u0442 \u0444\u0438\u043a\u0441\u0438\u0440\u043e\u0432\u0430\u043d\u043d\u043e\u0435 \u043a\u043e\u043b\u0438\u0447\u0435\u0441\u0442\u0432\u043e \u0433\u043e\u0440\u0443\u0442\u0438\u043d, \u043a\u043e\u0442\u043e\u0440\u044b\u0435 \u043e\u0431\u0440\u0430\u0431\u0430\u0442\u044b\u0432\u0430\u044e\u0442 \u0437\u0430\u0434\u0430\u0447\u0438 \u0438\u0437 \u043e\u0431\u0449\u0435\u0439 \u043e\u0447\u0435\u0440\u0435\u0434\u0438. \u042d\u0442\u043e\u0442 \u0448\u0430\u0431\u043b\u043e\u043d \u043f\u043e\u043b\u0435\u0437\u0435\u043d \u0434\u043b\u044f \u0443\u043f\u0440\u0430\u0432\u043b\u0435\u043d\u0438\u044f \u043a\u043e\u043b\u0438\u0447\u0435\u0441\u0442\u0432\u043e\u043c \u043e\u0434\u043d\u043e\u0432\u0440\u0435\u043c\u0435\u043d\u043d\u043e \u0432\u044b\u043f\u043e\u043b\u043d\u044f\u0435\u043c\u044b\u0445 \u0437\u0430\u0434\u0430\u0447 \u0438 \u043e\u043f\u0442\u0438\u043c\u0438\u0437\u0430\u0446\u0438\u0438 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u043d\u0438\u044f \u0440\u0435\u0441\u0443\u0440\u0441\u043e\u0432.<\/p>\n<pre><code class=\"go\">package main  import ( \"fmt\" \"sync\" \"time\" )  func worker(id int, jobs &lt;-chan int, results chan&lt;- int, wg *sync.WaitGroup) { defer wg.Done() for job := range jobs { fmt.Printf(\"\u0412\u043e\u0440\u043a\u0435\u0440 %d \u043d\u0430\u0447\u0430\u043b \u0437\u0430\u0434\u0430\u0447\u0443 %d\\n\", id, job) time.Sleep(time.Second) fmt.Printf(\"\u0412\u043e\u0440\u043a\u0435\u0440 %d \u0437\u0430\u0432\u0435\u0440\u0448\u0438\u043b \u0437\u0430\u0434\u0430\u0447\u0443 %d\\n\", id, job) results &lt;- job * 2 } }  func main() { const numJobs = 5 const numWorkers = 3 jobs := make(chan int, numJobs) results := make(chan int, numJobs) var wg sync.WaitGroup  for i := 1; i &lt;= numWorkers; i++ { wg.Add(1) go worker(i, jobs, results, &amp;wg) }  for j := 1; j &lt;= numJobs; j++ { jobs &lt;- j } close(jobs)  wg.Wait() close(results)  for result := range results { fmt.Println(\"\u0420\u0435\u0437\u0443\u043b\u044c\u0442\u0430\u0442:\", result) } } <\/code><\/pre>\n<p><strong>\u0420\u0435\u0430\u043b\u044c\u043d\u044b\u0439 \u0441\u0446\u0435\u043d\u0430\u0440\u0438\u0439<\/strong>: \u0412\u0435\u0431-\u0441\u0435\u0440\u0432\u0435\u0440, \u043e\u0431\u0440\u0430\u0431\u0430\u0442\u044b\u0432\u0430\u044e\u0449\u0438\u0439 \u0432\u0445\u043e\u0434\u044f\u0449\u0438\u0435 HTTP-\u0437\u0430\u043f\u0440\u043e\u0441\u044b, \u0433\u0434\u0435 \u043a\u0430\u0436\u0434\u044b\u0439 \u0437\u0430\u043f\u0440\u043e\u0441 \u043e\u0431\u0440\u0430\u0431\u0430\u0442\u044b\u0432\u0430\u0435\u0442\u0441\u044f \u0432\u043e\u0440\u043a\u0435\u0440\u043e\u043c \u0438\u0437 \u043f\u0443\u043b\u0430.<\/p>\n<hr\/>\n<h3>2. Fan-Out \/ Fan-In<\/h3>\n<p><strong>\u041e\u043f\u0438\u0441\u0430\u043d\u0438\u0435<\/strong>: Fan-Out \u0437\u0430\u043f\u0443\u0441\u043a\u0430\u0435\u0442 \u043d\u0435\u0441\u043a\u043e\u043b\u044c\u043a\u043e \u0433\u043e\u0440\u0443\u0442\u0438\u043d \u0434\u043b\u044f \u043f\u0430\u0440\u0430\u043b\u043b\u0435\u043b\u044c\u043d\u043e\u0439 \u043e\u0431\u0440\u0430\u0431\u043e\u0442\u043a\u0438 \u0434\u0430\u043d\u043d\u044b\u0445, \u0430 Fan-In \u0441\u043e\u0431\u0438\u0440\u0430\u0435\u0442 \u0440\u0435\u0437\u0443\u043b\u044c\u0442\u0430\u0442\u044b \u0432 \u0435\u0434\u0438\u043d\u044b\u0439 \u043a\u0430\u043d\u0430\u043b. \u042d\u0442\u043e\u0442 \u0448\u0430\u0431\u043b\u043e\u043d \u043f\u043e\u043b\u0435\u0437\u0435\u043d \u0434\u043b\u044f \u043f\u0430\u0440\u0430\u043b\u043b\u0435\u043b\u044c\u043d\u043e\u0439 \u043e\u0431\u0440\u0430\u0431\u043e\u0442\u043a\u0438 \u0441 \u043f\u043e\u0441\u043b\u0435\u0434\u0443\u044e\u0449\u0435\u0439 \u0430\u0433\u0440\u0435\u0433\u0430\u0446\u0438\u0435\u0439.<\/p>\n<pre><code class=\"go\">package main  import ( \"fmt\" \"sync\" )  func producer(id int, ch chan&lt;- int, wg *sync.WaitGroup) { defer wg.Done() for i := 0; i &lt; 5; i++ { ch &lt;- i fmt.Printf(\"\u041f\u0440\u043e\u0438\u0437\u0432\u043e\u0434\u0438\u0442\u0435\u043b\u044c %d \u0441\u043e\u0437\u0434\u0430\u043b %d\\n\", id, i) } } func consumer(id int, in &lt;-chan int, out chan&lt;- int, wg *sync.WaitGroup) { defer wg.Done() for v := range in { out &lt;- v * 2 fmt.Printf(\"\u041f\u043e\u0442\u0440\u0435\u0431\u0438\u0442\u0435\u043b\u044c %d \u043e\u0431\u0440\u0430\u0431\u043e\u0442\u0430\u043b %d\\n\", id, v) } } func main() { numProducers := 2 numConsumers := 2 input := make(chan int, 10) output := make(chan int, 10) var wg sync.WaitGroup for i := 1; i &lt;= numProducers; i++ { wg.Add(1) go producer(i, input, &amp;wg) } wg.Wait() close(input) for i := 1; i &lt;= numConsumers; i++ { wg.Add(1) go consumer(i, input, output, &amp;wg) } wg.Wait() close(output) for result := range output { fmt.Println(\"\u0420\u0435\u0437\u0443\u043b\u044c\u0442\u0430\u0442:\", result) } } <\/code><\/pre>\n<p><strong>\u0420\u0435\u0430\u043b\u044c\u043d\u044b\u0439 \u0441\u0446\u0435\u043d\u0430\u0440\u0438\u0439<\/strong>: \u041a\u043e\u043d\u0432\u0435\u0439\u0435\u0440 \u043e\u0431\u0440\u0430\u0431\u043e\u0442\u043a\u0438 \u0434\u0430\u043d\u043d\u044b\u0445, \u0433\u0434\u0435 \u0440\u0430\u0437\u043d\u044b\u0435 \u044d\u0442\u0430\u043f\u044b \u0432\u044b\u043f\u043e\u043b\u043d\u044f\u044e\u0442\u0441\u044f \u0440\u0430\u0437\u043d\u044b\u043c\u0438 \u043d\u0430\u0431\u043e\u0440\u0430\u043c\u0438 \u0432\u043e\u0440\u043a\u0435\u0440\u043e\u0432.<\/p>\n<hr\/>\n<h3>3. \u041f\u0430\u0439\u043f\u043b\u0430\u0439\u043d<\/h3>\n<p><strong>\u041e\u043f\u0438\u0441\u0430\u043d\u0438\u0435<\/strong>: \u041f\u0430\u0439\u043f\u043b\u0430\u0439\u043d \u043e\u0431\u044a\u0435\u0434\u0438\u043d\u044f\u0435\u0442 \u043d\u0435\u0441\u043a\u043e\u043b\u044c\u043a\u043e \u044d\u0442\u0430\u043f\u043e\u0432 \u043e\u0431\u0440\u0430\u0431\u043e\u0442\u043a\u0438, \u0433\u0434\u0435 \u043a\u0430\u0436\u0434\u044b\u0439 \u044d\u0442\u0430\u043f \u0432\u044b\u043f\u043e\u043b\u043d\u044f\u0435\u0442 \u043f\u0440\u0435\u043e\u0431\u0440\u0430\u0437\u043e\u0432\u0430\u043d\u0438\u0435 \u0434\u0430\u043d\u043d\u044b\u0445 \u0438 \u043f\u0435\u0440\u0435\u0434\u0430\u0451\u0442 \u0438\u0445 \u0441\u043b\u0435\u0434\u0443\u044e\u0449\u0435\u043c\u0443 \u044d\u0442\u0430\u043f\u0443. \u041f\u043e\u0434\u0445\u043e\u0434\u0438\u0442 \u0434\u043b\u044f \u043f\u043e\u0441\u043b\u0435\u0434\u043e\u0432\u0430\u0442\u0435\u043b\u044c\u043d\u043e\u0439 \u043e\u0431\u0440\u0430\u0431\u043e\u0442\u043a\u0438 \u0434\u0430\u043d\u043d\u044b\u0445.<\/p>\n<pre><code class=\"go\">package main  import \"fmt\"  func stage1(nums []int) &lt;-chan int { out := make(chan int) go func() { for _, n := range nums { out &lt;- n } close(out) }() return out }  func stage2(in &lt;-chan int) &lt;-chan int { out := make(chan int) go func() { for n := range in { out &lt;- n * 2 } close(out) }() return out }  func stage3(in &lt;-chan int) &lt;-chan int { out := make(chan int) go func() { for n := range in { out &lt;- n + 1 } close(out) }() return out }  func main() { nums := []int{1, 2, 3, 4, 5} c1 := stage1(nums) c2 := stage2(c1) c3 := stage3(c2) for result := range c3 { fmt.Println(result) } } <\/code><\/pre>\n<p><strong>\u0420\u0435\u0430\u043b\u044c\u043d\u044b\u0439 \u0441\u0446\u0435\u043d\u0430\u0440\u0438\u0439<\/strong>: \u0421\u0438\u0441\u0442\u0435\u043c\u0430 \u043e\u0431\u0440\u0430\u0431\u043e\u0442\u043a\u0438 \u0438\u0437\u043e\u0431\u0440\u0430\u0436\u0435\u043d\u0438\u0439, \u0433\u0434\u0435 \u0438\u0437\u043e\u0431\u0440\u0430\u0436\u0435\u043d\u0438\u0435 \u043f\u0440\u043e\u0445\u043e\u0434\u0438\u0442 \u0447\u0435\u0440\u0435\u0437 \u044d\u0442\u0430\u043f\u044b \u043c\u0430\u0441\u0448\u0442\u0430\u0431\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u044f, \u0444\u0438\u043b\u044c\u0442\u0440\u0430\u0446\u0438\u0438 \u0438 \u043a\u043e\u0434\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u044f.<\/p>\n<hr\/>\n<h3>4. \u041f\u0443\u0431\u043b\u0438\u043a\u0430\u0446\u0438\u044f-\u041f\u043e\u0434\u043f\u0438\u0441\u043a\u0430<\/h3>\n<p><strong>\u041e\u043f\u0438\u0441\u0430\u043d\u0438\u0435<\/strong>: \u0428\u0430\u0431\u043b\u043e\u043d &#171;\u041f\u0443\u0431\u043b\u0438\u043a\u0430\u0446\u0438\u044f-\u041f\u043e\u0434\u043f\u0438\u0441\u043a\u0430&#187; \u043f\u043e\u0437\u0432\u043e\u043b\u044f\u0435\u0442 \u043f\u0443\u0431\u043b\u0438\u043a\u043e\u0432\u0430\u0442\u044c \u0441\u043e\u043e\u0431\u0449\u0435\u043d\u0438\u044f \u0434\u043b\u044f \u043d\u0435\u0441\u043a\u043e\u043b\u044c\u043a\u0438\u0445 \u043f\u043e\u0434\u043f\u0438\u0441\u0447\u0438\u043a\u043e\u0432. \u041f\u043e\u043b\u0435\u0437\u0435\u043d \u0432 \u0441\u0438\u0441\u0442\u0435\u043c\u0430\u0445, \u0433\u0434\u0435 \u0440\u0430\u0437\u043d\u044b\u0435 \u0441\u0435\u0440\u0432\u0438\u0441\u044b \u0434\u043e\u043b\u0436\u043d\u044b \u043d\u0435\u0437\u0430\u0432\u0438\u0441\u0438\u043c\u043e \u0440\u0435\u0430\u0433\u0438\u0440\u043e\u0432\u0430\u0442\u044c \u043d\u0430 \u0441\u043e\u0431\u044b\u0442\u0438\u044f.<\/p>\n<pre><code class=\"go\">package main  import ( \"fmt\" \"sync\" \"time\" )  type PubSub struct { mu       sync.Mutex channels map[string][]chan string }  func NewPubSub() *PubSub { return &amp;PubSub{ channels: make(map[string][]chan string), } }  func (ps *PubSub) Subscribe(topic string) &lt;-chan string { ch := make(chan string) ps.mu.Lock() ps.channels[topic] = append(ps.channels[topic], ch) ps.mu.Unlock() return ch }  func (ps *PubSub) Publish(topic, msg string) { ps.mu.Lock() for _, ch := range ps.channels[topic] { ch &lt;- msg } ps.mu.Unlock() }  func (ps *PubSub) Close(topic string) { ps.mu.Lock() for _, ch := range ps.channels[topic] { close(ch) } ps.mu.Unlock() }  func main() { ps := NewPubSub()  subscriber1 := ps.Subscribe(\"news\") subscriber2 := ps.Subscribe(\"news\")  var wg sync.WaitGroup wg.Add(2)  go func() { defer wg.Done() for msg := range subscriber1 { fmt.Println(\"\u041f\u043e\u0434\u043f\u0438\u0441\u0447\u0438\u043a 1 \u043f\u043e\u043b\u0443\u0447\u0438\u043b:\", msg) } }()  go func() { defer wg.Done() for msg := range subscriber2 { fmt.Println(\"\u041f\u043e\u0434\u043f\u0438\u0441\u0447\u0438\u043a 2 \u043f\u043e\u043b\u0443\u0447\u0438\u043b:\", msg) } }()  ps.Publish(\"news\", \"\u0421\u0440\u043e\u0447\u043d\u044b\u0435 \u043d\u043e\u0432\u043e\u0441\u0442\u0438!\") ps.Publish(\"news\", \"\u0415\u0449\u0451 \u043d\u043e\u0432\u043e\u0441\u0442\u0438!\")  time.Sleep(time.Second) ps.Close(\"news\") wg.Wait() } <\/code><\/pre>\n<p><strong>\u0420\u0435\u0430\u043b\u044c\u043d\u044b\u0439 \u0441\u0446\u0435\u043d\u0430\u0440\u0438\u0439<\/strong>: \u0421\u0438\u0441\u0442\u0435\u043c\u0430 \u043e\u0431\u043c\u0435\u043d\u0430 \u0441\u043e\u043e\u0431\u0449\u0435\u043d\u0438\u044f\u043c\u0438, \u0433\u0434\u0435 \u0441\u0435\u0440\u0432\u0438\u0441\u044b \u043f\u043e\u0434\u043f\u0438\u0441\u044b\u0432\u0430\u044e\u0442\u0441\u044f \u043d\u0430 \u043e\u043f\u0440\u0435\u0434\u0435\u043b\u0451\u043d\u043d\u044b\u0435 \u0442\u0438\u043f\u044b \u0441\u043e\u0431\u044b\u0442\u0438\u0439.<\/p>\n<hr\/>\n<h3>5. Select \u0441 \u0442\u0430\u0439\u043c\u0430\u0443\u0442\u043e\u043c<\/h3>\n<p><strong>\u041e\u043f\u0438\u0441\u0430\u043d\u0438\u0435<\/strong>: \u0418\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u043d\u0438\u0435 <code>select<\/code> \u0441 \u0442\u0430\u0439\u043c\u0430\u0443\u0442\u043e\u043c \u043f\u043e\u0437\u0432\u043e\u043b\u044f\u0435\u0442 \u0438\u0437\u0431\u0435\u0436\u0430\u0442\u044c \u0431\u0435\u0441\u043a\u043e\u043d\u0435\u0447\u043d\u044b\u0445 \u0431\u043b\u043e\u043a\u0438\u0440\u043e\u0432\u043e\u043a. \u041f\u043e\u043b\u0435\u0437\u043d\u043e, \u043a\u043e\u0433\u0434\u0430 \u043d\u0443\u0436\u043d\u043e \u0432\u044b\u043f\u043e\u043b\u043d\u0438\u0442\u044c \u0434\u0435\u0439\u0441\u0442\u0432\u0438\u0435 \u0438\u043b\u0438 \u043f\u0440\u0435\u0440\u0432\u0430\u0442\u044c \u043e\u043f\u0435\u0440\u0430\u0446\u0438\u044e, \u0435\u0441\u043b\u0438 \u043e\u043d\u0430 \u0437\u0430\u043d\u0438\u043c\u0430\u0435\u0442 \u0441\u043b\u0438\u0448\u043a\u043e\u043c \u043c\u043d\u043e\u0433\u043e \u0432\u0440\u0435\u043c\u0435\u043d\u0438.<\/p>\n<pre><code class=\"go\">package main  import ( \"fmt\" \"time\" )  func main() { c := make(chan string)  go func() { time.Sleep(2 * time.Second) c &lt;- \"\u0440\u0435\u0437\u0443\u043b\u044c\u0442\u0430\u0442\" }()  select { case res := &lt;-c: fmt.Println(\"\u041f\u043e\u043b\u0443\u0447\u0435\u043d\u043e:\", res) case &lt;-time.After(1 * time.Second): fmt.Println(\"\u0422\u0430\u0439\u043c\u0430\u0443\u0442\") } } <\/code><\/pre>\n<p><strong>\u0420\u0435\u0430\u043b\u044c\u043d\u044b\u0439 \u0441\u0446\u0435\u043d\u0430\u0440\u0438\u0439<\/strong>: \u0421\u0435\u0442\u0435\u0432\u043e\u0439 \u043a\u043b\u0438\u0435\u043d\u0442, \u043a\u043e\u0442\u043e\u0440\u044b\u0439 \u043f\u044b\u0442\u0430\u0435\u0442\u0441\u044f \u043f\u043e\u0434\u043a\u043b\u044e\u0447\u0438\u0442\u044c\u0441\u044f \u043a \u0441\u0435\u0440\u0432\u0435\u0440\u0443 \u0438 \u043e\u0441\u0442\u0430\u043d\u0430\u0432\u043b\u0438\u0432\u0430\u0435\u0442\u0441\u044f, \u0435\u0441\u043b\u0438 \u0441\u0435\u0440\u0432\u0435\u0440 \u043d\u0435 \u043e\u0442\u0432\u0435\u0447\u0430\u0435\u0442 \u0432\u043e\u0432\u0440\u0435\u043c\u044f.<\/p>\n<hr\/>\n<h3>6. \u0421\u0435\u043c\u0430\u0444\u043e\u0440<\/h3>\n<p><strong>\u041e\u043f\u0438\u0441\u0430\u043d\u0438\u0435<\/strong>: \u0421\u0435\u043c\u0430\u0444\u043e\u0440 \u043e\u0433\u0440\u0430\u043d\u0438\u0447\u0438\u0432\u0430\u0435\u0442 \u043a\u043e\u043b\u0438\u0447\u0435\u0441\u0442\u0432\u043e \u0433\u043e\u0440\u0443\u0442\u0438\u043d, \u043a\u043e\u0442\u043e\u0440\u044b\u0435 \u043c\u043e\u0433\u0443\u0442 \u043e\u0434\u043d\u043e\u0432\u0440\u0435\u043c\u0435\u043d\u043d\u043e \u043e\u0431\u0440\u0430\u0449\u0430\u0442\u044c\u0441\u044f \u043a \u0440\u0435\u0441\u0443\u0440\u0441\u0443. \u041f\u043e\u043b\u0435\u0437\u0435\u043d \u0434\u043b\u044f \u0443\u043f\u0440\u0430\u0432\u043b\u0435\u043d\u0438\u044f \u043a\u043e\u043d\u043a\u0443\u0440\u0435\u043d\u0442\u043d\u043e\u0441\u0442\u044c\u044e \u0438 \u043f\u0440\u0435\u0434\u043e\u0442\u0432\u0440\u0430\u0449\u0435\u043d\u0438\u044f \u043f\u0435\u0440\u0435\u0433\u0440\u0443\u0437\u043a\u0438 \u0440\u0435\u0441\u0443\u0440\u0441\u043e\u0432.<\/p>\n<pre><code class=\"go\">package main  import ( \"fmt\" \"sync\" \"time\" )  func worker(id int, sem chan struct{}, wg *sync.WaitGroup) { defer wg.Done() sem &lt;- struct{}{} \/\/ \u0417\u0430\u0445\u0432\u0430\u0442 \u0441\u0435\u043c\u0430\u0444\u043e\u0440\u0430 fmt.Printf(\"\u0412\u043e\u0440\u043a\u0435\u0440 %d \u043d\u0430\u0447\u0430\u043b\\n\", id) time.Sleep(time.Second) fmt.Printf(\"\u0412\u043e\u0440\u043a\u0435\u0440 %d \u0437\u0430\u0432\u0435\u0440\u0448\u0438\u043b\\n\", id) &lt;-sem \/\/ \u041e\u0441\u0432\u043e\u0431\u043e\u0436\u0434\u0435\u043d\u0438\u0435 \u0441\u0435\u043c\u0430\u0444\u043e\u0440\u0430 }  func main() { const numWorkers = 5 const maxConcurrent = 2 sem := make(chan struct{}, maxConcurrent) var wg sync.WaitGroup  for i := 1; i &lt;= numWorkers; i++ { wg.Add(1) go worker(i, sem, &amp;wg) }  wg.Wait() } <\/code><\/pre>\n<p><strong>\u0420\u0435\u0430\u043b\u044c\u043d\u044b\u0439 \u0441\u0446\u0435\u043d\u0430\u0440\u0438\u0439<\/strong>: \u041f\u0443\u043b \u043f\u043e\u0434\u043a\u043b\u044e\u0447\u0435\u043d\u0438\u0439 \u043a \u0431\u0430\u0437\u0435 \u0434\u0430\u043d\u043d\u044b\u0445, \u0433\u0434\u0435 \u043e\u0434\u043d\u043e\u0432\u0440\u0435\u043c\u0435\u043d\u043d\u043e \u0434\u043e\u043f\u0443\u0441\u043a\u0430\u0435\u0442\u0441\u044f \u043e\u0433\u0440\u0430\u043d\u0438\u0447\u0435\u043d\u043d\u043e\u0435 \u043a\u043e\u043b\u0438\u0447\u0435\u0441\u0442\u0432\u043e \u043f\u043e\u0434\u043a\u043b\u044e\u0447\u0435\u043d\u0438\u0439.<\/p>\n<hr\/>\n<h3>7. \u041e\u0433\u0440\u0430\u043d\u0438\u0447\u0435\u043d\u0438\u0435 \u0447\u0430\u0441\u0442\u043e\u0442\u044b<\/h3>\n<p><strong>\u041e\u043f\u0438\u0441\u0430\u043d\u0438\u0435<\/strong>: \u041e\u0433\u0440\u0430\u043d\u0438\u0447\u0435\u043d\u0438\u0435 \u0447\u0430\u0441\u0442\u043e\u0442\u044b \u0443\u043f\u0440\u0430\u0432\u043b\u044f\u0435\u0442 \u0441\u043a\u043e\u0440\u043e\u0441\u0442\u044c\u044e \u043e\u0431\u0440\u0430\u0431\u043e\u0442\u043a\u0438 \u0441\u043e\u0431\u044b\u0442\u0438\u0439 \u0441 \u043f\u043e\u043c\u043e\u0449\u044c\u044e \u0442\u0438\u043a\u0435\u0440\u0430. \u041f\u043e\u043b\u0435\u0437\u043d\u043e, \u043a\u043e\u0433\u0434\u0430 \u043d\u0443\u0436\u043d\u043e \u043a\u043e\u043d\u0442\u0440\u043e\u043b\u0438\u0440\u043e\u0432\u0430\u0442\u044c \u0447\u0430\u0441\u0442\u043e\u0442\u0443 \u0432\u044b\u043f\u043e\u043b\u043d\u0435\u043d\u0438\u044f \u0437\u0430\u0434\u0430\u0447, \u043d\u0430\u043f\u0440\u0438\u043c\u0435\u0440, \u0437\u0430\u043f\u0440\u043e\u0441\u043e\u0432 \u043a API.<\/p>\n<pre><code class=\"go\">package main  import ( \"fmt\" \"time\" )  func main() { rate := time.Second ticker := time.NewTicker(rate) defer ticker.Stop()  requests := make(chan int, 5) for i := 1; i &lt;= 5; i++ { requests &lt;- i } close(requests)  for req := range requests { &lt;-ticker.C \/\/ \u041e\u0436\u0438\u0434\u0430\u043d\u0438\u0435 \u0441\u043b\u0435\u0434\u0443\u044e\u0449\u0435\u0433\u043e \u0442\u0438\u043a\u0430 fmt.Println(\"\u041e\u0431\u0440\u0430\u0431\u043e\u0442\u043a\u0430 \u0437\u0430\u043f\u0440\u043e\u0441\u0430\", req) } }<\/code><\/pre>\n<p><strong>\u0420\u0435\u0430\u043b\u044c\u043d\u044b\u0439 \u0441\u0446\u0435\u043d\u0430\u0440\u0438\u0439<\/strong>: \u0428\u043b\u044e\u0437 API, \u043e\u0433\u0440\u0430\u043d\u0438\u0447\u0438\u0432\u0430\u044e\u0449\u0438\u0439 \u043a\u043e\u043b\u0438\u0447\u0435\u0441\u0442\u0432\u043e \u0437\u0430\u043f\u0440\u043e\u0441\u043e\u0432, \u043a\u043e\u0442\u043e\u0440\u044b\u0435 \u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u0435\u043b\u044c \u043c\u043e\u0436\u0435\u0442 \u0441\u0434\u0435\u043b\u0430\u0442\u044c \u0437\u0430 \u043e\u043f\u0440\u0435\u0434\u0435\u043b\u0451\u043d\u043d\u044b\u0439 \u043f\u0435\u0440\u0438\u043e\u0434.<\/p>\n<h3>\u0417\u0430\u043a\u043b\u044e\u0447\u0435\u043d\u0438\u0435<\/h3>\n<p>\u0428\u0430\u0431\u043b\u043e\u043d\u044b \u043a\u043e\u043d\u043a\u0443\u0440\u0435\u043d\u0442\u043d\u043e\u0441\u0442\u0438 \u0432 Go \u043d\u0435\u043e\u0431\u0445\u043e\u0434\u0438\u043c\u044b \u0434\u043b\u044f \u0441\u043e\u0437\u0434\u0430\u043d\u0438\u044f \u044d\u0444\u0444\u0435\u043a\u0442\u0438\u0432\u043d\u044b\u0445 \u0438 \u043c\u0430\u0441\u0448\u0442\u0430\u0431\u0438\u0440\u0443\u0435\u043c\u044b\u0445 \u043f\u0440\u0438\u043b\u043e\u0436\u0435\u043d\u0438\u0439. \u041e\u0441\u0432\u043e\u0435\u043d\u0438\u0435 \u044d\u0442\u0438\u0445 \u0448\u0430\u0431\u043b\u043e\u043d\u043e\u0432 \u043f\u043e\u0437\u0432\u043e\u043b\u0438\u0442 \u0432\u0430\u043c \u044d\u0444\u0444\u0435\u043a\u0442\u0438\u0432\u043d\u043e \u0443\u043f\u0440\u0430\u0432\u043b\u044f\u0442\u044c \u043a\u043e\u043d\u043a\u0443\u0440\u0435\u043d\u0442\u043d\u043e\u0441\u0442\u044c\u044e, \u043e\u043f\u0442\u0438\u043c\u0438\u0437\u0438\u0440\u043e\u0432\u0430\u0442\u044c \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u043d\u0438\u0435 \u0440\u0435\u0441\u0443\u0440\u0441\u043e\u0432 \u0438 \u043f\u043e\u0432\u044b\u0441\u0438\u0442\u044c \u043f\u0440\u043e\u0438\u0437\u0432\u043e\u0434\u0438\u0442\u0435\u043b\u044c\u043d\u043e\u0441\u0442\u044c \u0432\u0430\u0448\u0438\u0445 \u043f\u0440\u0438\u043b\u043e\u0436\u0435\u043d\u0438\u0439.<\/p>\n<\/div>\n<\/div>\n<\/div>\n<p><!----><!----><\/div>\n<p><!----><!----><br \/> \u0441\u0441\u044b\u043b\u043a\u0430 \u043d\u0430 \u043e\u0440\u0438\u0433\u0438\u043d\u0430\u043b \u0441\u0442\u0430\u0442\u044c\u0438 <a href=\"https:\/\/habr.com\/ru\/articles\/919754\/\"> https:\/\/habr.com\/ru\/articles\/919754\/<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<div><!--[--><!--]--><\/div>\n<div id=\"post-content-body\">\n<div>\n<div class=\"article-formatted-body article-formatted-body article-formatted-body_version-2\">\n<div xmlns=\"http:\/\/www.w3.org\/1999\/xhtml\">\n<p>\u041a\u043e\u043d\u043a\u0443\u0440\u0435\u043d\u0442\u043d\u043e\u0441\u0442\u044c \u2014 \u043e\u0434\u043d\u0430 \u0438\u0437 \u0441\u0430\u043c\u044b\u0445 \u043c\u043e\u0449\u043d\u044b\u0445 \u0432\u043e\u0437\u043c\u043e\u0436\u043d\u043e\u0441\u0442\u0435\u0439 Go, \u0438 \u0435\u0451 \u043e\u0441\u0432\u043e\u0435\u043d\u0438\u0435 \u043a\u0440\u0438\u0442\u0438\u0447\u0435\u0441\u043a\u0438 \u0432\u0430\u0436\u043d\u043e \u0434\u043b\u044f \u0441\u043e\u0437\u0434\u0430\u043d\u0438\u044f \u043c\u0430\u0441\u0448\u0442\u0430\u0431\u0438\u0440\u0443\u0435\u043c\u044b\u0445 \u0438 \u044d\u0444\u0444\u0435\u043a\u0442\u0438\u0432\u043d\u044b\u0445 \u043f\u0440\u0438\u043b\u043e\u0436\u0435\u043d\u0438\u0439. \u0412 \u044d\u0442\u043e\u0439 \u0441\u0442\u0430\u0442\u044c\u0435 \u043c\u044b \u0440\u0430\u0441\u0441\u043c\u043e\u0442\u0440\u0438\u043c 7 \u0448\u0430\u0431\u043b\u043e\u043d\u043e\u0432 \u043a\u043e\u043d\u043a\u0443\u0440\u0435\u043d\u0442\u043d\u043e\u0441\u0442\u0438 \u0432 Go, \u043a\u043e\u0442\u043e\u0440\u044b\u0435 \u043f\u043e\u043c\u043e\u0433\u0443\u0442 \u0432\u0430\u043c \u043f\u0438\u0441\u0430\u0442\u044c \u043d\u0430\u0434\u0451\u0436\u043d\u044b\u0439 \u043a\u043e\u0434.<\/p>\n<h3>1. \u041f\u0443\u043b \u0432\u043e\u0440\u043a\u0435\u0440\u043e\u0432<\/h3>\n<p><strong>\u041e\u043f\u0438\u0441\u0430\u043d\u0438\u0435<\/strong>: \u041f\u0443\u043b \u0432\u043e\u0440\u043a\u0435\u0440\u043e\u0432 \u0441\u043e\u0437\u0434\u0430\u0451\u0442 \u0444\u0438\u043a\u0441\u0438\u0440\u043e\u0432\u0430\u043d\u043d\u043e\u0435 \u043a\u043e\u043b\u0438\u0447\u0435\u0441\u0442\u0432\u043e \u0433\u043e\u0440\u0443\u0442\u0438\u043d, \u043a\u043e\u0442\u043e\u0440\u044b\u0435 \u043e\u0431\u0440\u0430\u0431\u0430\u0442\u044b\u0432\u0430\u044e\u0442 \u0437\u0430\u0434\u0430\u0447\u0438 \u0438\u0437 \u043e\u0431\u0449\u0435\u0439 \u043e\u0447\u0435\u0440\u0435\u0434\u0438. \u042d\u0442\u043e\u0442 \u0448\u0430\u0431\u043b\u043e\u043d \u043f\u043e\u043b\u0435\u0437\u0435\u043d \u0434\u043b\u044f \u0443\u043f\u0440\u0430\u0432\u043b\u0435\u043d\u0438\u044f \u043a\u043e\u043b\u0438\u0447\u0435\u0441\u0442\u0432\u043e\u043c \u043e\u0434\u043d\u043e\u0432\u0440\u0435\u043c\u0435\u043d\u043d\u043e \u0432\u044b\u043f\u043e\u043b\u043d\u044f\u0435\u043c\u044b\u0445 \u0437\u0430\u0434\u0430\u0447 \u0438 \u043e\u043f\u0442\u0438\u043c\u0438\u0437\u0430\u0446\u0438\u0438 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u043d\u0438\u044f \u0440\u0435\u0441\u0443\u0440\u0441\u043e\u0432.<\/p>\n<pre><code class=\"go\">package main  import ( \"fmt\" \"sync\" \"time\" )  func worker(id int, jobs &lt;-chan int, results chan&lt;- int, wg *sync.WaitGroup) { defer wg.Done() for job := range jobs { fmt.Printf(\"\u0412\u043e\u0440\u043a\u0435\u0440 %d \u043d\u0430\u0447\u0430\u043b \u0437\u0430\u0434\u0430\u0447\u0443 %d\\n\", id, job) time.Sleep(time.Second) fmt.Printf(\"\u0412\u043e\u0440\u043a\u0435\u0440 %d \u0437\u0430\u0432\u0435\u0440\u0448\u0438\u043b \u0437\u0430\u0434\u0430\u0447\u0443 %d\\n\", id, job) results &lt;- job * 2 } }  func main() { const numJobs = 5 const numWorkers = 3 jobs := make(chan int, numJobs) results := make(chan int, numJobs) var wg sync.WaitGroup  for i := 1; i &lt;= numWorkers; i++ { wg.Add(1) go worker(i, jobs, results, &amp;wg) }  for j := 1; j &lt;= numJobs; j++ { jobs &lt;- j } close(jobs)  wg.Wait() close(results)  for result := range results { fmt.Println(\"\u0420\u0435\u0437\u0443\u043b\u044c\u0442\u0430\u0442:\", result) } } <\/code><\/pre>\n<p><strong>\u0420\u0435\u0430\u043b\u044c\u043d\u044b\u0439 \u0441\u0446\u0435\u043d\u0430\u0440\u0438\u0439<\/strong>: \u0412\u0435\u0431-\u0441\u0435\u0440\u0432\u0435\u0440, \u043e\u0431\u0440\u0430\u0431\u0430\u0442\u044b\u0432\u0430\u044e\u0449\u0438\u0439 \u0432\u0445\u043e\u0434\u044f\u0449\u0438\u0435 HTTP-\u0437\u0430\u043f\u0440\u043e\u0441\u044b, \u0433\u0434\u0435 \u043a\u0430\u0436\u0434\u044b\u0439 \u0437\u0430\u043f\u0440\u043e\u0441 \u043e\u0431\u0440\u0430\u0431\u0430\u0442\u044b\u0432\u0430\u0435\u0442\u0441\u044f \u0432\u043e\u0440\u043a\u0435\u0440\u043e\u043c \u0438\u0437 \u043f\u0443\u043b\u0430.<\/p>\n<hr\/>\n<h3>2. Fan-Out \/ Fan-In<\/h3>\n<p><strong>\u041e\u043f\u0438\u0441\u0430\u043d\u0438\u0435<\/strong>: Fan-Out \u0437\u0430\u043f\u0443\u0441\u043a\u0430\u0435\u0442 \u043d\u0435\u0441\u043a\u043e\u043b\u044c\u043a\u043e \u0433\u043e\u0440\u0443\u0442\u0438\u043d \u0434\u043b\u044f \u043f\u0430\u0440\u0430\u043b\u043b\u0435\u043b\u044c\u043d\u043e\u0439 \u043e\u0431\u0440\u0430\u0431\u043e\u0442\u043a\u0438 \u0434\u0430\u043d\u043d\u044b\u0445, \u0430 Fan-In \u0441\u043e\u0431\u0438\u0440\u0430\u0435\u0442 \u0440\u0435\u0437\u0443\u043b\u044c\u0442\u0430\u0442\u044b \u0432 \u0435\u0434\u0438\u043d\u044b\u0439 \u043a\u0430\u043d\u0430\u043b. \u042d\u0442\u043e\u0442 \u0448\u0430\u0431\u043b\u043e\u043d \u043f\u043e\u043b\u0435\u0437\u0435\u043d \u0434\u043b\u044f \u043f\u0430\u0440\u0430\u043b\u043b\u0435\u043b\u044c\u043d\u043e\u0439 \u043e\u0431\u0440\u0430\u0431\u043e\u0442\u043a\u0438 \u0441 \u043f\u043e\u0441\u043b\u0435\u0434\u0443\u044e\u0449\u0435\u0439 \u0430\u0433\u0440\u0435\u0433\u0430\u0446\u0438\u0435\u0439.<\/p>\n<pre><code class=\"go\">package main  import ( \"fmt\" \"sync\" )  func producer(id int, ch chan&lt;- int, wg *sync.WaitGroup) { defer wg.Done() for i := 0; i &lt; 5; i++ { ch &lt;- i fmt.Printf(\"\u041f\u0440\u043e\u0438\u0437\u0432\u043e\u0434\u0438\u0442\u0435\u043b\u044c %d \u0441\u043e\u0437\u0434\u0430\u043b %d\\n\", id, i) } } func consumer(id int, in &lt;-chan int, out chan&lt;- int, wg *sync.WaitGroup) { defer wg.Done() for v := range in { out &lt;- v * 2 fmt.Printf(\"\u041f\u043e\u0442\u0440\u0435\u0431\u0438\u0442\u0435\u043b\u044c %d \u043e\u0431\u0440\u0430\u0431\u043e\u0442\u0430\u043b %d\\n\", id, v) } } func main() { numProducers := 2 numConsumers := 2 input := make(chan int, 10) output := make(chan int, 10) var wg sync.WaitGroup for i := 1; i &lt;= numProducers; i++ { wg.Add(1) go producer(i, input, &amp;wg) } wg.Wait() close(input) for i := 1; i &lt;= numConsumers; i++ { wg.Add(1) go consumer(i, input, output, &amp;wg) } wg.Wait() close(output) for result := range output { fmt.Println(\"\u0420\u0435\u0437\u0443\u043b\u044c\u0442\u0430\u0442:\", result) } } <\/code><\/pre>\n<p><strong>\u0420\u0435\u0430\u043b\u044c\u043d\u044b\u0439 \u0441\u0446\u0435\u043d\u0430\u0440\u0438\u0439<\/strong>: \u041a\u043e\u043d\u0432\u0435\u0439\u0435\u0440 \u043e\u0431\u0440\u0430\u0431\u043e\u0442\u043a\u0438 \u0434\u0430\u043d\u043d\u044b\u0445, \u0433\u0434\u0435 \u0440\u0430\u0437\u043d\u044b\u0435 \u044d\u0442\u0430\u043f\u044b \u0432\u044b\u043f\u043e\u043b\u043d\u044f\u044e\u0442\u0441\u044f \u0440\u0430\u0437\u043d\u044b\u043c\u0438 \u043d\u0430\u0431\u043e\u0440\u0430\u043c\u0438 \u0432\u043e\u0440\u043a\u0435\u0440\u043e\u0432.<\/p>\n<hr\/>\n<h3>3. \u041f\u0430\u0439\u043f\u043b\u0430\u0439\u043d<\/h3>\n<p><strong>\u041e\u043f\u0438\u0441\u0430\u043d\u0438\u0435<\/strong>: \u041f\u0430\u0439\u043f\u043b\u0430\u0439\u043d \u043e\u0431\u044a\u0435\u0434\u0438\u043d\u044f\u0435\u0442 \u043d\u0435\u0441\u043a\u043e\u043b\u044c\u043a\u043e \u044d\u0442\u0430\u043f\u043e\u0432 \u043e\u0431\u0440\u0430\u0431\u043e\u0442\u043a\u0438, \u0433\u0434\u0435 \u043a\u0430\u0436\u0434\u044b\u0439 \u044d\u0442\u0430\u043f \u0432\u044b\u043f\u043e\u043b\u043d\u044f\u0435\u0442 \u043f\u0440\u0435\u043e\u0431\u0440\u0430\u0437\u043e\u0432\u0430\u043d\u0438\u0435 \u0434\u0430\u043d\u043d\u044b\u0445 \u0438 \u043f\u0435\u0440\u0435\u0434\u0430\u0451\u0442 \u0438\u0445 \u0441\u043b\u0435\u0434\u0443\u044e\u0449\u0435\u043c\u0443 \u044d\u0442\u0430\u043f\u0443. \u041f\u043e\u0434\u0445\u043e\u0434\u0438\u0442 \u0434\u043b\u044f \u043f\u043e\u0441\u043b\u0435\u0434\u043e\u0432\u0430\u0442\u0435\u043b\u044c\u043d\u043e\u0439 \u043e\u0431\u0440\u0430\u0431\u043e\u0442\u043a\u0438 \u0434\u0430\u043d\u043d\u044b\u0445.<\/p>\n<pre><code class=\"go\">package main  import \"fmt\"  func stage1(nums []int) &lt;-chan int { out := make(chan int) go func() { for _, n := range nums { out &lt;- n } close(out) }() return out }  func stage2(in &lt;-chan int) &lt;-chan int { out := make(chan int) go func() { for n := range in { out &lt;- n * 2 } close(out) }() return out }  func stage3(in &lt;-chan int) &lt;-chan int { out := make(chan int) go func() { for n := range in { out &lt;- n + 1 } close(out) }() return out }  func main() { nums := []int{1, 2, 3, 4, 5} c1 := stage1(nums) c2 := stage2(c1) c3 := stage3(c2) for result := range c3 { fmt.Println(result) } } <\/code><\/pre>\n<p><strong>\u0420\u0435\u0430\u043b\u044c\u043d\u044b\u0439 \u0441\u0446\u0435\u043d\u0430\u0440\u0438\u0439<\/strong>: \u0421\u0438\u0441\u0442\u0435\u043c\u0430 \u043e\u0431\u0440\u0430\u0431\u043e\u0442\u043a\u0438 \u0438\u0437\u043e\u0431\u0440\u0430\u0436\u0435\u043d\u0438\u0439, \u0433\u0434\u0435 \u0438\u0437\u043e\u0431\u0440\u0430\u0436\u0435\u043d\u0438\u0435 \u043f\u0440\u043e\u0445\u043e\u0434\u0438\u0442 \u0447\u0435\u0440\u0435\u0437 \u044d\u0442\u0430\u043f\u044b \u043c\u0430\u0441\u0448\u0442\u0430\u0431\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u044f, \u0444\u0438\u043b\u044c\u0442\u0440\u0430\u0446\u0438\u0438 \u0438 \u043a\u043e\u0434\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u044f.<\/p>\n<hr\/>\n<h3>4. \u041f\u0443\u0431\u043b\u0438\u043a\u0430\u0446\u0438\u044f-\u041f\u043e\u0434\u043f\u0438\u0441\u043a\u0430<\/h3>\n<p><strong>\u041e\u043f\u0438\u0441\u0430\u043d\u0438\u0435<\/strong>: \u0428\u0430\u0431\u043b\u043e\u043d &#171;\u041f\u0443\u0431\u043b\u0438\u043a\u0430\u0446\u0438\u044f-\u041f\u043e\u0434\u043f\u0438\u0441\u043a\u0430&#187; \u043f\u043e\u0437\u0432\u043e\u043b\u044f\u0435\u0442 \u043f\u0443\u0431\u043b\u0438\u043a\u043e\u0432\u0430\u0442\u044c \u0441\u043e\u043e\u0431\u0449\u0435\u043d\u0438\u044f \u0434\u043b\u044f \u043d\u0435\u0441\u043a\u043e\u043b\u044c\u043a\u0438\u0445 \u043f\u043e\u0434\u043f\u0438\u0441\u0447\u0438\u043a\u043e\u0432. \u041f\u043e\u043b\u0435\u0437\u0435\u043d \u0432 \u0441\u0438\u0441\u0442\u0435\u043c\u0430\u0445, \u0433\u0434\u0435 \u0440\u0430\u0437\u043d\u044b\u0435 \u0441\u0435\u0440\u0432\u0438\u0441\u044b \u0434\u043e\u043b\u0436\u043d\u044b \u043d\u0435\u0437\u0430\u0432\u0438\u0441\u0438\u043c\u043e \u0440\u0435\u0430\u0433\u0438\u0440\u043e\u0432\u0430\u0442\u044c \u043d\u0430 \u0441\u043e\u0431\u044b\u0442\u0438\u044f.<\/p>\n<pre><code class=\"go\">package main  import ( \"fmt\" \"sync\" \"time\" )  type PubSub struct { mu       sync.Mutex channels map[string][]chan string }  func NewPubSub() *PubSub { return &amp;PubSub{ channels: make(map[string][]chan string), } }  func (ps *PubSub) Subscribe(topic string) &lt;-chan string { ch := make(chan string) ps.mu.Lock() ps.channels[topic] = append(ps.channels[topic], ch) ps.mu.Unlock() return ch }  func (ps *PubSub) Publish(topic, msg string) { ps.mu.Lock() for _, ch := range ps.channels[topic] { ch &lt;- msg } ps.mu.Unlock() }  func (ps *PubSub) Close(topic string) { ps.mu.Lock() for _, ch := range ps.channels[topic] { close(ch) } ps.mu.Unlock() }  func main() { ps := NewPubSub()  subscriber1 := ps.Subscribe(\"news\") subscriber2 := ps.Subscribe(\"news\")  var wg sync.WaitGroup wg.Add(2)  go func() { defer wg.Done() for msg := range subscriber1 { fmt.Println(\"\u041f\u043e\u0434\u043f\u0438\u0441\u0447\u0438\u043a 1 \u043f\u043e\u043b\u0443\u0447\u0438\u043b:\", msg) } }()  go func() { defer wg.Done() for msg := range subscriber2 { fmt.Println(\"\u041f\u043e\u0434\u043f\u0438\u0441\u0447\u0438\u043a 2 \u043f\u043e\u043b\u0443\u0447\u0438\u043b:\", msg) } }()  ps.Publish(\"news\", \"\u0421\u0440\u043e\u0447\u043d\u044b\u0435 \u043d\u043e\u0432\u043e\u0441\u0442\u0438!\") ps.Publish(\"news\", \"\u0415\u0449\u0451 \u043d\u043e\u0432\u043e\u0441\u0442\u0438!\")  time.Sleep(time.Second) ps.Close(\"news\") wg.Wait() } <\/code><\/pre>\n<p><strong>\u0420\u0435\u0430\u043b\u044c\u043d\u044b\u0439 \u0441\u0446\u0435\u043d\u0430\u0440\u0438\u0439<\/strong>: \u0421\u0438\u0441\u0442\u0435\u043c\u0430 \u043e\u0431\u043c\u0435\u043d\u0430 \u0441\u043e\u043e\u0431\u0449\u0435\u043d\u0438\u044f\u043c\u0438, \u0433\u0434\u0435 \u0441\u0435\u0440\u0432\u0438\u0441\u044b \u043f\u043e\u0434\u043f\u0438\u0441\u044b\u0432\u0430\u044e\u0442\u0441\u044f \u043d\u0430 \u043e\u043f\u0440\u0435\u0434\u0435\u043b\u0451\u043d\u043d\u044b\u0435 \u0442\u0438\u043f\u044b \u0441\u043e\u0431\u044b\u0442\u0438\u0439.<\/p>\n<hr\/>\n<h3>5. Select \u0441 \u0442\u0430\u0439\u043c\u0430\u0443\u0442\u043e\u043c<\/h3>\n<p><strong>\u041e\u043f\u0438\u0441\u0430\u043d\u0438\u0435<\/strong>: \u0418\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u043d\u0438\u0435 <code>select<\/code> \u0441 \u0442\u0430\u0439\u043c\u0430\u0443\u0442\u043e\u043c \u043f\u043e\u0437\u0432\u043e\u043b\u044f\u0435\u0442 \u0438\u0437\u0431\u0435\u0436\u0430\u0442\u044c \u0431\u0435\u0441\u043a\u043e\u043d\u0435\u0447\u043d\u044b\u0445 \u0431\u043b\u043e\u043a\u0438\u0440\u043e\u0432\u043e\u043a. \u041f\u043e\u043b\u0435\u0437\u043d\u043e, \u043a\u043e\u0433\u0434\u0430 \u043d\u0443\u0436\u043d\u043e \u0432\u044b\u043f\u043e\u043b\u043d\u0438\u0442\u044c \u0434\u0435\u0439\u0441\u0442\u0432\u0438\u0435 \u0438\u043b\u0438 \u043f\u0440\u0435\u0440\u0432\u0430\u0442\u044c \u043e\u043f\u0435\u0440\u0430\u0446\u0438\u044e, \u0435\u0441\u043b\u0438 \u043e\u043d\u0430 \u0437\u0430\u043d\u0438\u043c\u0430\u0435\u0442 \u0441\u043b\u0438\u0448\u043a\u043e\u043c \u043c\u043d\u043e\u0433\u043e \u0432\u0440\u0435\u043c\u0435\u043d\u0438.<\/p>\n<pre><code class=\"go\">package main  import ( \"fmt\" \"time\" )  func main() { c := make(chan string)  go func() { time.Sleep(2 * time.Second) c &lt;- \"\u0440\u0435\u0437\u0443\u043b\u044c\u0442\u0430\u0442\" }()  select { case res := &lt;-c: fmt.Println(\"\u041f\u043e\u043b\u0443\u0447\u0435\u043d\u043e:\", res) case &lt;-time.After(1 * time.Second): fmt.Println(\"\u0422\u0430\u0439\u043c\u0430\u0443\u0442\") } } <\/code><\/pre>\n<p><strong>\u0420\u0435\u0430\u043b\u044c\u043d\u044b\u0439 \u0441\u0446\u0435\u043d\u0430\u0440\u0438\u0439<\/strong>: \u0421\u0435\u0442\u0435\u0432\u043e\u0439 \u043a\u043b\u0438\u0435\u043d\u0442, \u043a\u043e\u0442\u043e\u0440\u044b\u0439 \u043f\u044b\u0442\u0430\u0435\u0442\u0441\u044f \u043f\u043e\u0434\u043a\u043b\u044e\u0447\u0438\u0442\u044c\u0441\u044f \u043a \u0441\u0435\u0440\u0432\u0435\u0440\u0443 \u0438 \u043e\u0441\u0442\u0430\u043d\u0430\u0432\u043b\u0438\u0432\u0430\u0435\u0442\u0441\u044f, \u0435\u0441\u043b\u0438 \u0441\u0435\u0440\u0432\u0435\u0440 \u043d\u0435 \u043e\u0442\u0432\u0435\u0447\u0430\u0435\u0442 \u0432\u043e\u0432\u0440\u0435\u043c\u044f.<\/p>\n<hr\/>\n<h3>6. \u0421\u0435\u043c\u0430\u0444\u043e\u0440<\/h3>\n<p><strong>\u041e\u043f\u0438\u0441\u0430\u043d\u0438\u0435<\/strong>: \u0421\u0435\u043c\u0430\u0444\u043e\u0440 \u043e\u0433\u0440\u0430\u043d\u0438\u0447\u0438\u0432\u0430\u0435\u0442 \u043a\u043e\u043b\u0438\u0447\u0435\u0441\u0442\u0432\u043e \u0433\u043e\u0440\u0443\u0442\u0438\u043d, \u043a\u043e\u0442\u043e\u0440\u044b\u0435 \u043c\u043e\u0433\u0443\u0442 \u043e\u0434\u043d\u043e\u0432\u0440\u0435\u043c\u0435\u043d\u043d\u043e \u043e\u0431\u0440\u0430\u0449\u0430\u0442\u044c\u0441\u044f \u043a \u0440\u0435\u0441\u0443\u0440\u0441\u0443. \u041f\u043e\u043b\u0435\u0437\u0435\u043d \u0434\u043b\u044f \u0443\u043f\u0440\u0430\u0432\u043b\u0435\u043d\u0438\u044f \u043a\u043e\u043d\u043a\u0443\u0440\u0435\u043d\u0442\u043d\u043e\u0441\u0442\u044c\u044e \u0438 \u043f\u0440\u0435\u0434\u043e\u0442\u0432\u0440\u0430\u0449\u0435\u043d\u0438\u044f \u043f\u0435\u0440\u0435\u0433\u0440\u0443\u0437\u043a\u0438 \u0440\u0435\u0441\u0443\u0440\u0441\u043e\u0432.<\/p>\n<pre><code class=\"go\">package main  import ( \"fmt\" \"sync\" \"time\" )  func worker(id int, sem chan struct{}, wg *sync.WaitGroup) { defer wg.Done() sem &lt;- struct{}{} \/\/ \u0417\u0430\u0445\u0432\u0430\u0442 \u0441\u0435\u043c\u0430\u0444\u043e\u0440\u0430 fmt.Printf(\"\u0412\u043e\u0440\u043a\u0435\u0440 %d \u043d\u0430\u0447\u0430\u043b\\n\", id) time.Sleep(time.Second) fmt.Printf(\"\u0412\u043e\u0440\u043a\u0435\u0440 %d \u0437\u0430\u0432\u0435\u0440\u0448\u0438\u043b\\n\", id) &lt;-sem \/\/ \u041e\u0441\u0432\u043e\u0431\u043e\u0436\u0434\u0435\u043d\u0438\u0435 \u0441\u0435\u043c\u0430\u0444\u043e\u0440\u0430 }  func main() { const numWorkers = 5 const maxConcurrent = 2 sem := make(chan struct{}, maxConcurrent) var wg sync.WaitGroup  for i := 1; i &lt;= numWorkers; i++ { wg.Add(1) go worker(i, sem, &amp;wg) }  wg.Wait() } <\/code><\/pre>\n<p><strong>\u0420\u0435\u0430\u043b\u044c\u043d\u044b\u0439 \u0441\u0446\u0435\u043d\u0430\u0440\u0438\u0439<\/strong>: \u041f\u0443\u043b \u043f\u043e\u0434\u043a\u043b\u044e\u0447\u0435\u043d\u0438\u0439 \u043a \u0431\u0430\u0437\u0435 \u0434\u0430\u043d\u043d\u044b\u0445, \u0433\u0434\u0435 \u043e\u0434\u043d\u043e\u0432\u0440\u0435\u043c\u0435\u043d\u043d\u043e \u0434\u043e\u043f\u0443\u0441\u043a\u0430\u0435\u0442\u0441\u044f \u043e\u0433\u0440\u0430\u043d\u0438\u0447\u0435\u043d\u043d\u043e\u0435 \u043a\u043e\u043b\u0438\u0447\u0435\u0441\u0442\u0432\u043e \u043f\u043e\u0434\u043a\u043b\u044e\u0447\u0435\u043d\u0438\u0439.<\/p>\n<hr\/>\n<h3>7. \u041e\u0433\u0440\u0430\u043d\u0438\u0447\u0435\u043d\u0438\u0435 \u0447\u0430\u0441\u0442\u043e\u0442\u044b<\/h3>\n<p><strong>\u041e\u043f\u0438\u0441\u0430\u043d\u0438\u0435<\/strong>: \u041e\u0433\u0440\u0430\u043d\u0438\u0447\u0435\u043d\u0438\u0435 \u0447\u0430\u0441\u0442\u043e\u0442\u044b \u0443\u043f\u0440\u0430\u0432\u043b\u044f\u0435\u0442 \u0441\u043a\u043e\u0440\u043e\u0441\u0442\u044c\u044e \u043e\u0431\u0440\u0430\u0431\u043e\u0442\u043a\u0438 \u0441\u043e\u0431\u044b\u0442\u0438\u0439 \u0441 \u043f\u043e\u043c\u043e\u0449\u044c\u044e \u0442\u0438\u043a\u0435\u0440\u0430. \u041f\u043e\u043b\u0435\u0437\u043d\u043e, \u043a\u043e\u0433\u0434\u0430 \u043d\u0443\u0436\u043d\u043e \u043a\u043e\u043d\u0442\u0440\u043e\u043b\u0438\u0440\u043e\u0432\u0430\u0442\u044c \u0447\u0430\u0441\u0442\u043e\u0442\u0443 \u0432\u044b\u043f\u043e\u043b\u043d\u0435\u043d\u0438\u044f \u0437\u0430\u0434\u0430\u0447, \u043d\u0430\u043f\u0440\u0438\u043c\u0435\u0440, \u0437\u0430\u043f\u0440\u043e\u0441\u043e\u0432 \u043a API.<\/p>\n<pre><code class=\"go\">package main  import ( \"fmt\" \"time\" )  func main() { rate := time.Second ticker := time.NewTicker(rate) defer ticker.Stop()  requests := make(chan int, 5) for i := 1; i &lt;= 5; i++ { requests &lt;- i } close(requests)  for req := range requests { &lt;-ticker.C \/\/ \u041e\u0436\u0438\u0434\u0430\u043d\u0438\u0435 \u0441\u043b\u0435\u0434\u0443\u044e\u0449\u0435\u0433\u043e \u0442\u0438\u043a\u0430 fmt.Println(\"\u041e\u0431\u0440\u0430\u0431\u043e\u0442\u043a\u0430 \u0437\u0430\u043f\u0440\u043e\u0441\u0430\", req) } }<\/code><\/pre>\n<p><strong>\u0420\u0435\u0430\u043b\u044c\u043d\u044b\u0439 \u0441\u0446\u0435\u043d\u0430\u0440\u0438\u0439<\/strong>: \u0428\u043b\u044e\u0437 API, \u043e\u0433\u0440\u0430\u043d\u0438\u0447\u0438\u0432\u0430\u044e\u0449\u0438\u0439 \u043a\u043e\u043b\u0438\u0447\u0435\u0441\u0442\u0432\u043e \u0437\u0430\u043f\u0440\u043e\u0441\u043e\u0432, \u043a\u043e\u0442\u043e\u0440\u044b\u0435 \u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u0435\u043b\u044c \u043c\u043e\u0436\u0435\u0442 \u0441\u0434\u0435\u043b\u0430\u0442\u044c \u0437\u0430 \u043e\u043f\u0440\u0435\u0434\u0435\u043b\u0451\u043d\u043d\u044b\u0439 \u043f\u0435\u0440\u0438\u043e\u0434.<\/p>\n<h3>\u0417\u0430\u043a\u043b\u044e\u0447\u0435\u043d\u0438\u0435<\/h3>\n<p>\u0428\u0430\u0431\u043b\u043e\u043d\u044b \u043a\u043e\u043d\u043a\u0443\u0440\u0435\u043d\u0442\u043d\u043e\u0441\u0442\u0438 \u0432 Go \u043d\u0435\u043e\u0431\u0445\u043e\u0434\u0438\u043c\u044b \u0434\u043b\u044f \u0441\u043e\u0437\u0434\u0430\u043d\u0438\u044f \u044d\u0444\u0444\u0435\u043a\u0442\u0438\u0432\u043d\u044b\u0445 \u0438 \u043c\u0430\u0441\u0448\u0442\u0430\u0431\u0438\u0440\u0443\u0435\u043c\u044b\u0445 \u043f\u0440\u0438\u043b\u043e\u0436\u0435\u043d\u0438\u0439. \u041e\u0441\u0432\u043e\u0435\u043d\u0438\u0435 \u044d\u0442\u0438\u0445 \u0448\u0430\u0431\u043b\u043e\u043d\u043e\u0432 \u043f\u043e\u0437\u0432\u043e\u043b\u0438\u0442 \u0432\u0430\u043c \u044d\u0444\u0444\u0435\u043a\u0442\u0438\u0432\u043d\u043e \u0443\u043f\u0440\u0430\u0432\u043b\u044f\u0442\u044c \u043a\u043e\u043d\u043a\u0443\u0440\u0435\u043d\u0442\u043d\u043e\u0441\u0442\u044c\u044e, \u043e\u043f\u0442\u0438\u043c\u0438\u0437\u0438\u0440\u043e\u0432\u0430\u0442\u044c \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u043d\u0438\u0435 \u0440\u0435\u0441\u0443\u0440\u0441\u043e\u0432 \u0438 \u043f\u043e\u0432\u044b\u0441\u0438\u0442\u044c \u043f\u0440\u043e\u0438\u0437\u0432\u043e\u0434\u0438\u0442\u0435\u043b\u044c\u043d\u043e\u0441\u0442\u044c \u0432\u0430\u0448\u0438\u0445 \u043f\u0440\u0438\u043b\u043e\u0436\u0435\u043d\u0438\u0439.<\/p>\n<\/div>\n<\/div>\n<\/div>\n<p><!----><!----><\/div>\n<p><!----><!----><br \/> \u0441\u0441\u044b\u043b\u043a\u0430 \u043d\u0430 \u043e\u0440\u0438\u0433\u0438\u043d\u0430\u043b \u0441\u0442\u0430\u0442\u044c\u0438 <a href=\"https:\/\/habr.com\/ru\/articles\/919754\/\"> https:\/\/habr.com\/ru\/articles\/919754\/<\/a><br \/><\/br><\/br><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[],"tags":[],"class_list":["post-463824","post","type-post","status-publish","format-standard","hentry"],"_links":{"self":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/posts\/463824","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=463824"}],"version-history":[{"count":0,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/posts\/463824\/revisions"}],"wp:attachment":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=463824"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=463824"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=463824"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}