{"id":453850,"date":"2025-03-31T09:00:30","date_gmt":"2025-03-31T09:00:30","guid":{"rendered":"http:\/\/savepearlharbor.com\/?p=453850"},"modified":"-0001-11-30T00:00:00","modified_gmt":"-0001-11-29T21:00:00","slug":"","status":"publish","type":"post","link":"https:\/\/savepearlharbor.com\/?p=453850","title":{"rendered":"<span>\u0420\u0430\u0437\u0431\u0438\u0440\u0430\u0435\u043c \u043f\u0430\u0442\u0442\u0435\u0440\u043d\u044b \u043a\u043e\u043d\u043a\u0443\u0440\u0435\u043d\u0442\u043d\u043e\u0441\u0442\u0438<\/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<figure class=\"full-width\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/habrastorage.org\/r\/w1560\/getpro\/habr\/upload_files\/05c\/30c\/5e7\/05c30c5e726b933a70e0b5703e78088a.png\" width=\"2118\" height=\"1188\" data-src=\"https:\/\/habrastorage.org\/getpro\/habr\/upload_files\/05c\/30c\/5e7\/05c30c5e726b933a70e0b5703e78088a.png\"\/><\/figure>\n<h2>\u0411\u0430\u0437\u0430<\/h2>\n<p>\u041f\u0430\u0440\u0430\u043b\u043b\u0435\u043b\u044c\u043d\u043e\u0441\u0442\u044c &#8212; \u0432\u044b\u043f\u043e\u043b\u043d\u0435\u043d\u0438\u0435 \u0437\u0430\u0434\u0430\u0447 \u0432 \u043e\u0434\u0438\u043d \u043c\u043e\u043c\u0435\u043d\u0442 \u0432\u0440\u0435\u043c\u0435\u043d\u0438 \u043d\u0430 \u0440\u0430\u0437\u043d\u044b\u0445 \u043b\u043e\u0433\u0438\u0447\u0435\u0441\u043a\u0438\u0445 \u044f\u0434\u0440\u0430\u0445.<br \/>\u041a\u043e\u043d\u043a\u0443\u0440\u0435\u043d\u0442\u043d\u043e\u0441\u0442\u044c &#8212; \u0432\u044b\u043f\u043e\u043b\u043d\u0435\u043d\u0438\u0435 \u0437\u0430\u0434\u0430\u0447 \u043f\u043e\u0441\u043b\u0435\u0434\u043e\u0432\u0430\u0442\u0435\u043b\u044c\u043d\u043e, \u043d\u043e \u0441\u043e \u0441\u043c\u0435\u043d\u043e\u0439 \u043a\u043e\u043d\u0442\u0435\u043a\u0441\u0442\u0430 \u043d\u0430 \u0434\u0440\u0443\u0433\u0443\u044e \u0437\u0430\u0434\u0430\u0447\u0443 \u0432 \u043e\u0436\u0438\u0434\u0430\u043d\u0438\u0435 \u0437\u0430\u0432\u0435\u0440\u0448\u0435\u043d\u0438\u044f \u0438\u043d\u043e\u0439 \u0437\u0430\u0434\u0430\u0447\u0438. \u0423 \u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u0435\u043b\u044f \u043c\u043e\u0436\u0435\u0442 \u0432\u043e\u0437\u043d\u0438\u043a\u043d\u0443\u0442\u044c \u0438\u043b\u043b\u044e\u0437\u0438\u044f \u043c\u043d\u043e\u0433\u043e\u0437\u0430\u0434\u0430\u0447\u043d\u043e\u0441\u0442\u0438 \u0434\u0430\u0436\u0435 \u0432 \u043e\u0434\u043d\u043e\u043f\u0440\u043e\u0446\u0435\u0441\u0441\u043e\u0440\u043d\u043e\u0439 \u0441\u0438\u0441\u0442\u0435\u043c\u0435, \u043f\u043e\u0441\u043a\u043e\u043b\u044c\u043a\u0443 \u0441\u043c\u0435\u043d\u0430 \u043a\u043e\u043d\u0442\u0435\u043a\u0441\u0442\u0430 \u043f\u0440\u043e\u0438\u0441\u0445\u043e\u0434\u0438\u0442 \u0431\u044b\u0441\u0442\u0440\u043e (\u043c\u0438\u043a\u0440\u043e\u0441\u0435\u043a\u0443\u043d\u0434\u044b).<\/p>\n<p>\u041f\u0440\u043e\u0446\u0435\u0441\u0441\u044b:<\/p>\n<ul>\n<li>\n<p>\u0420\u0430\u0437\u0434\u0435\u043b\u044c\u043d\u0430\u044f \u043f\u0430\u043c\u044f\u0442\u044c<\/p>\n<\/li>\n<li>\n<p>\u0420\u0430\u0437\u0434\u0435\u043b\u044c\u043d\u044b\u0435 \u0440\u0435\u0441\u0443\u0440\u0441\u044b<\/p>\n<\/li>\n<li>\n<p>\u0420\u0430\u0437\u0434\u0435\u043b\u044c\u043d\u044b\u0435 \u0440\u0435\u0433\u0438\u0441\u0442\u0440\u044b<\/p>\n<\/li>\n<\/ul>\n<p>\u041f\u043e\u0442\u043e\u043a\u0438:<\/p>\n<ul>\n<li>\n<p>\u041e\u0431\u0449\u0430\u044f \u043f\u0430\u043c\u044f\u0442\u044c<\/p>\n<\/li>\n<li>\n<p>\u041e\u0431\u0449\u0438\u0435 \u0440\u0435\u0441\u0443\u0440\u0441\u044b<\/p>\n<\/li>\n<li>\n<p>\u0420\u0430\u0437\u0434\u0435\u043b\u044c\u043d\u044b\u0435 \u0441\u0442\u044d\u043a \u0438 \u0440\u0435\u0433\u0438\u0441\u0442\u0440\u044b<\/p>\n<\/li>\n<\/ul>\n<p>\u0413\u043e\u0440\u0443\u0442\u0438\u043d\u044b:<\/p>\n<ul>\n<li>\n<p>\u041e\u0431\u0449\u0430\u044f \u043f\u0430\u043c\u044f\u0442\u044c<\/p>\n<\/li>\n<li>\n<p>\u041e\u0431\u0449\u0438\u0435 \u0440\u0435\u0441\u0443\u0440\u0441\u044b<\/p>\n<\/li>\n<li>\n<p>\u041e\u0431\u0449\u0438\u0439 \u0441\u0438\u0441\u0442\u0435\u043c\u043d\u044b\u0439 \u0441\u0442\u044d\u043a<\/p>\n<\/li>\n<li>\n<p>\u041e\u0431\u0449\u0438\u0435 \u0440\u0435\u0433\u0438\u0441\u0442\u0440\u044b<\/p>\n<\/li>\n<\/ul>\n<p>Go runtime \u043f\u0440\u0435\u0434\u0441\u0442\u0430\u0432\u043b\u044f\u0435\u0442 \u043c\u043e\u0434\u0435\u043b\u044c P:M:G.<br \/> P &#8212; \u043f\u0440\u0435\u0434\u0441\u0442\u0430\u0432\u043b\u044f\u0435\u0442 \u043b\u043e\u0433\u0438\u0447\u0435\u0441\u043a\u043e\u0435 \u044f\u0434\u0440\u043e \u043f\u0440\u043e\u0446\u0435\u0441\u0441\u043e\u0440\u0430.<br \/> M &#8212; \u043f\u043e\u0442\u043e\u043a \u041e\u0421 \u043f\u043e \u0447\u0438\u0441\u043b\u0443 \u043f\u0440\u043e\u0446\u0435\u0441\u0441\u043e\u0440\u043e\u0432 P.<br \/> G &#8212; \u0441\u0442\u0440\u0443\u043a\u0442\u0443\u0440\u0430, \u043a\u043e\u0442\u043e\u0440\u0430\u044f \u0432\u044b\u043f\u043e\u043b\u043d\u044f\u0435\u0442 \u043f\u0435\u0440\u0435\u0434\u0430\u043d\u043d\u0443\u044e \u0444\u0443\u043d\u043a\u0446\u0438\u044e, \u0441\u043e\u0437\u0434\u0430\u0451\u0442\u0441\u044f \u043f\u043e \u043d\u0435\u043e\u0431\u0445\u043e\u0434\u0438\u043c\u043e\u0441\u0442\u0438, \u043c\u0438\u043d\u0438\u043c\u0443\u043c \u043e\u0434\u043d\u0430 \u043d\u0430 \u0441\u0442\u0430\u0440\u0442\u0435 \u043f\u0440\u043e\u0433\u0440\u0430\u043c\u043c\u044b (main). \u0421\u0442\u044d\u043a \u0432\u0441\u0435\u0433\u043e 2\u043a\u0431, \u043c\u043e\u0436\u0435\u0442 \u0440\u0430\u0441\u0448\u0438\u0440\u044f\u0442\u0441\u044f \u0434\u043e 1\u0433\u0431 \u0434\u043b\u044f 64x \u0438 \u0434\u043e 250\u043a\u0431 \u0434\u043b\u044f 32\u0445 \u0441\u0438\u0441\u0442\u0435\u043c.<\/p>\n<p>\u0423\u043f\u0440\u0430\u0432\u043b\u0435\u043d\u0438\u0435 \u0433\u043e\u0440\u0443\u0442\u0438\u043d\u0430\u043c\u0438 \u043e\u0441\u0443\u0449\u0435\u0441\u0442\u0432\u043b\u044f\u0435\u0442\u0441\u044f \u043f\u043b\u0430\u043d\u0438\u0440\u043e\u0432\u0449\u0438\u043a\u043e\u043c Go, \u0430 \u043d\u0435 \u041e\u0421. \u041f\u043b\u0430\u043d\u0438\u0440\u043e\u0432\u0449\u0438\u043a Go \u0440\u0430\u0431\u043e\u0442\u0430\u0435\u0442 \u0432 \u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u0435\u043b\u044c\u0441\u043a\u043e\u043c \u043f\u0440\u043e\u0441\u0442\u0440\u0430\u043d\u0441\u0442\u0432\u0435. \u041c\u044b \u043d\u0435 \u043c\u043e\u0436\u0435\u043c \u043d\u0430\u043f\u0440\u044f\u043c\u0443\u044e \u0443\u043f\u0440\u0430\u0432\u043b\u044f\u0442\u044c \u043d\u0430 \u043a\u0430\u043a\u043e\u043c \u043f\u0440\u043e\u0446\u0435\u0441\u0441\u043e\u0440\u0435 \u0431\u0443\u0434\u0435\u0442 \u0438\u0441\u043f\u043e\u043b\u043d\u044f\u0442\u044c\u0441\u044f \u0433\u043e\u0440\u0443\u0442\u0438\u043d\u0430, \u0437\u0430 \u044d\u0442\u043e \u043e\u0442\u0432\u0435\u0447\u0430\u0435\u0442 \u043f\u043b\u0430\u043d\u0438\u0440\u043e\u0432\u0449\u0438\u043a.<\/p>\n<p>\u041a\u0430\u043d\u0430\u043b &#8212; \u043e\u0447\u0435\u0440\u0435\u0434\u044c \u0441\u043e\u043e\u0431\u0449\u0435\u043d\u0438\u0439, \u043a\u043e\u0442\u043e\u0440\u0430\u044f \u0443\u043c\u0435\u0435\u0442 \u0440\u0430\u0431\u043e\u0442\u0430\u0442\u044c \u0432 \u043c\u043d\u043e\u0433\u043e\u043f\u043e\u0442\u043e\u0447\u043d\u043e\u0439 \u0441\u0440\u0435\u0434\u0435, \u0440\u0430\u0431\u043e\u0442\u0430\u0435\u0442 \u043f\u043e \u043f\u0440\u0438\u043d\u0446\u0438\u043f\u0443 FIFO.<br \/>\u0415\u0441\u0442\u044c \u0434\u0432\u0430 \u0442\u0438\u043f\u0430 \u043a\u0430\u043d\u0430\u043b\u043e\u0432: \u0431\u0443\u0444\u0435\u0440\u0438\u0437\u043e\u0432\u0430\u043d\u043d\u044b\u0439 \u0438 \u043d\u0435\u0431\u0443\u0444\u0435\u0440\u0438\u0437\u043e\u0432\u0430\u043d\u043d\u044b\u0439.<br \/>\u041f\u0435\u0440\u0432\u044b\u0439 \u043c\u043e\u0436\u0435\u0442 \u0445\u0440\u0430\u043d\u0438\u0442\u044c \u043d\u0435\u0441\u043a\u043e\u043b\u044c\u043a\u043e \u0441\u043e\u043e\u0431\u0449\u0435\u043d\u0438\u0439, \u0432\u0442\u043e\u0440\u043e\u0439 \u0442\u043e\u043b\u044c\u043a\u043e \u043e\u0434\u043d\u043e.<\/p>\n<h2>\u0421\u0438\u043d\u0445\u0440\u043e\u043d\u0438\u0437\u0430\u0446\u0438\u044f<\/h2>\n<p>sync.WaitGroup &#8212; \u0441\u0447\u0451\u0442\u0447\u0438\u043a, \u043a\u043e\u0442\u043e\u0440\u044b\u0439 \u043f\u043e\u0437\u0432\u043e\u043b\u044f\u0435\u0442 \u043f\u043e\u0434\u043e\u0436\u0434\u0430\u0442\u044c \u0437\u0430\u0432\u0435\u0440\u0448\u0435\u043d\u0438\u044f \u0433\u043e\u0440\u0443\u0442\u0438\u043d.<br \/>sync.Mutex &#8212; \u0431\u043b\u043e\u043a\u0438\u0440\u0443\u0435\u0442 \u0434\u043e\u0441\u0442\u0443\u043f \u043a \u0440\u0435\u0441\u0443\u0440\u0441\u0443.<br \/>sync.RwMutex &#8212; \u0440\u0430\u0437\u0434\u0435\u043b\u044f\u0435\u043c\u0430\u044f \u0431\u043b\u043e\u043a\u0438\u0440\u043e\u0432\u043a\u0430 \u043d\u0430 \u0447\u0442\u0435\u043d\u0438\u0435 \u0438 \u0437\u0430\u043f\u0438\u0441\u044c. \u0427\u0438\u0442\u0430\u0442\u044c \u043c\u043e\u0433\u0443\u0442 \u043d\u0435\u0441\u043a\u043e\u043b\u044c\u043a\u043e \u0433\u043e\u0440\u0443\u0442\u0438\u043d, \u043d\u043e \u043c\u0443\u0442\u0438\u0440\u043e\u0432\u0430\u0442\u044c \u0434\u0430\u043d\u043d\u044b\u0435 \u0442\u043e\u043b\u044c\u043a\u043e \u043e\u0434\u043d\u0430.<br \/>sync.Atomic &#8212; \u0430\u0442\u043e\u043c\u0430\u0440\u043d\u0430\u044f \u043e\u043f\u0435\u0440\u0430\u0446\u0438\u044f \u0447\u0442\u0435\u043d\u0438\u044f \u0438 \u0437\u0430\u043f\u0438\u0441\u0438. \u0420\u0430\u0431\u043e\u0442\u0430\u0435\u0442 \u0442\u043e\u043b\u044c\u043a\u043e \u0441 \u043f\u0440\u043e\u0441\u0442\u044b\u043c\u0438 \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u044f\u043c\u0438.<br \/>sync.Map &#8212; lock-free \u0441\u0442\u0440\u0443\u043a\u0442\u0443\u0440\u0430. \u0420\u0430\u0431\u043e\u0442\u0430\u0435\u0442 \u0442\u0430\u043a \u0436\u0435, \u043a\u0430\u043a \u0438 \u043e\u0431\u044b\u0447\u043d\u0430\u044f map, \u043d\u043e \u043f\u043e\u0442\u043e\u043a\u043e\u0431\u0435\u0437\u043e\u043f\u0430\u0441\u043d\u0430\u044f, \u043c\u043e\u0436\u043d\u043e \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c \u0432 \u043c\u043d\u043e\u0433\u043e\u043f\u043e\u0442\u043e\u0447\u043d\u043e\u0439 \u0441\u0440\u0435\u0434\u0435. \u0425\u043e\u0440\u043e\u0448\u043e \u043f\u043e\u0434\u0445\u043e\u0434\u0438\u0442 \u0434\u043b\u044f \u0441\u043b\u0443\u0447\u0430\u0435\u0432, \u0433\u0434\u0435 \u043d\u0430\u0434\u043e \u043c\u043d\u043e\u0433\u043e \u0447\u0438\u0442\u0430\u0442\u044c \u0438 \u043c\u0430\u043b\u043e \u043f\u0438\u0441\u0430\u0442\u044c. \u0415\u0441\u043b\u0438 \u043d\u0430\u0434\u043e \u043c\u043d\u043e\u0433\u043e \u043f\u0438\u0441\u0430\u0442\u044c, \u0442\u043e \u043b\u0443\u0447\u0448\u0435 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c \u043e\u0431\u044b\u0447\u043d\u0443\u044e map \u0438 sync.RwMutex.<\/p>\n<h2>\u041d\u0435\u0431\u0443\u0444\u0435\u0440\u0438\u0437\u043e\u0432\u0430\u043d\u043d\u044b\u0439 \u043a\u0430\u043d\u0430\u043b<\/h2>\n<div>\n<div class=\"table\">\n<table>\n<tbody>\n<tr>\n<th>\n<p align=\"left\">\u0414\u0435\u0439\u0441\u0442\u0432\u0438\u0435<\/p>\n<\/th>\n<th>\n<p align=\"left\">\u041e\u0442\u043a\u0440\u044b\u0442\u044b\u0439 \u043a\u0430\u043d\u0430\u043b<\/p>\n<\/th>\n<th>\n<p align=\"left\">\u0417\u0430\u043a\u0440\u044b\u0442\u044b\u0439 \u043a\u0430\u043d\u0430\u043b<\/p>\n<\/th>\n<th>\n<p align=\"left\">\u041d\u0435\u0438\u043d\u0438\u0446\u0438\u0430\u043b\u0438\u0437\u0438\u0440\u043e\u0432\u0430\u043d\u043d\u044b\u0439 \u043a\u0430\u043d\u0430\u043b<\/p>\n<\/th>\n<\/tr>\n<tr>\n<td>\n<p align=\"left\">\u0427\u0442\u0435\u043d\u0438\u0435<\/p>\n<\/td>\n<td>\n<p align=\"left\">\u0411\u043b\u043e\u043a\u0438\u0440\u043e\u0432\u043a\u0430 \u0434\u043e \u043f\u0440\u0438\u0445\u043e\u0434\u0430 \u043f\u0438\u0441\u0430\u0442\u0435\u043b\u044f<\/p>\n<\/td>\n<td>\n<p align=\"left\">Zero value<\/p>\n<\/td>\n<td>\n<p align=\"left\">\u0411\u043b\u043e\u043a\u0438\u0440\u043e\u0432\u043a\u0430 \u043d\u0430\u0432\u0441\u0435\u0433\u0434\u0430<\/p>\n<\/td>\n<\/tr>\n<tr>\n<td>\n<p align=\"left\">\u0417\u0430\u043f\u0438\u0441\u044c<\/p>\n<\/td>\n<td>\n<p align=\"left\">\u0411\u043b\u043e\u043a\u0438\u0440\u043e\u0432\u043a\u0430 \u0434\u043e \u043f\u0440\u0438\u0445\u043e\u0434\u0430 \u0447\u0438\u0442\u0430\u0442\u0435\u043b\u044f<\/p>\n<\/td>\n<td>\n<p align=\"left\">Panic<\/p>\n<\/td>\n<td>\n<p align=\"left\">\u0411\u043b\u043e\u043a\u0438\u0440\u043e\u0432\u043a\u0430 \u043d\u0430\u0432\u0441\u0435\u0433\u0434\u0430<\/p>\n<\/td>\n<\/tr>\n<tr>\n<td>\n<p align=\"left\">\u0417\u0430\u043a\u0440\u044b\u0442\u0438\u0435<\/p>\n<\/td>\n<td>\n<p align=\"left\">\u041a\u0430\u043d\u0430\u043b \u0437\u0430\u043a\u0440\u043e\u0435\u0442\u0441\u044f<\/p>\n<\/td>\n<td>\n<p align=\"left\">Panic<\/p>\n<\/td>\n<td>\n<p align=\"left\">Panic<\/p>\n<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<\/div>\n<\/div>\n<h2>\u0411\u0443\u0444\u0435\u0440\u0438\u0437\u043e\u0432\u0430\u043d\u043d\u044b\u0439 \u043a\u0430\u043d\u0430\u043b<\/h2>\n<div>\n<div class=\"table\">\n<table>\n<tbody>\n<tr>\n<th>\n<p align=\"left\">\u0414\u0435\u0439\u0441\u0442\u0432\u0438\u0435<\/p>\n<\/th>\n<th>\n<p align=\"left\">\u041e\u0442\u043a\u0440\u044b\u0442\u044b\u0439 \u0438 \u0447\u0430\u0441\u0442\u0438\u0447\u043d\u043e \u0437\u0430\u043f\u043e\u043b\u043d\u0435\u043d\u043d\u044b\u0439<\/p>\n<\/th>\n<th>\n<p align=\"left\">\u041e\u0442\u043a\u0440\u044b\u0442\u044b\u0439 \u0438 \u043f\u043e\u043b\u043d\u043e\u0441\u0442\u044c\u044e \u0437\u0430\u043f\u043e\u043b\u043d\u0435\u043d\u043d\u044b\u0439<\/p>\n<\/th>\n<th>\n<p align=\"left\">\u041e\u0442\u043a\u0440\u044b\u0442\u044b\u0439 \u0438 \u043f\u0443\u0441\u0442\u043e\u0439<\/p>\n<\/th>\n<th>\n<p align=\"left\">\u0417\u0430\u043a\u0440\u044b\u0442\u044b\u0439 \u0438 \u0447\u0430\u0441\u0442\u0438\u0447\u043d\u043e \u0437\u0430\u043f\u043e\u043b\u043d\u0435\u043d\u043d\u044b\u0439<\/p>\n<\/th>\n<\/tr>\n<tr>\n<td>\n<p align=\"left\">\u0427\u0442\u0435\u043d\u0438\u0435<\/p>\n<\/td>\n<td>\n<p align=\"left\">\u041f\u0440\u043e\u0447\u0438\u0442\u0430\u0435\u043c \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0435<\/p>\n<\/td>\n<td>\n<p align=\"left\">\u041f\u0440\u043e\u0447\u0438\u0442\u0430\u0435\u043c \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0435<\/p>\n<\/td>\n<td>\n<p align=\"left\">\u0411\u043b\u043e\u043a\u0438\u0440\u043e\u0432\u043a\u0430 \u0434\u043e \u043f\u0440\u0438\u0445\u043e\u0434\u0430 \u043f\u0438\u0441\u0430\u0442\u0435\u043b\u044f<\/p>\n<\/td>\n<td>\n<p align=\"left\">\u041f\u0440\u043e\u0447\u0438\u0442\u0430\u0435\u043c \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0435<\/p>\n<\/td>\n<\/tr>\n<tr>\n<td>\n<p align=\"left\">\u0417\u0430\u043f\u0438\u0441\u044c<\/p>\n<\/td>\n<td>\n<p align=\"left\">\u0417\u0430\u043f\u0438\u0448\u0435\u043c \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0435<\/p>\n<\/td>\n<td>\n<p align=\"left\">\u0411\u043b\u043e\u043a\u0438\u0440\u043e\u0432\u043a\u0430 \u0434\u043e \u043f\u0440\u0438\u0445\u043e\u0434\u0430 \u0447\u0438\u0442\u0430\u0442\u0435\u043b\u044f<\/p>\n<\/td>\n<td>\n<p align=\"left\">\u0417\u0430\u043f\u0438\u0448\u0435\u043c \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0435<\/p>\n<\/td>\n<td>\n<p align=\"left\">Panic<\/p>\n<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<\/div>\n<\/div>\n<h2>\u041e\u0433\u0440\u0430\u043d\u0438\u0447\u0435\u043d\u0438\u044f \u043a\u0430\u043d\u0430\u043b\u0430<\/h2>\n<div>\n<div class=\"table\">\n<table>\n<tbody>\n<tr>\n<th>\n<p align=\"left\">\u0414\u0435\u0439\u0441\u0442\u0432\u0438\u0435<\/p>\n<\/th>\n<th>\n<p align=\"left\">\u041a\u0430\u043d\u0430\u043b \u0442\u043e\u043b\u044c\u043a\u043e \u043d\u0430 \u0447\u0442\u0435\u043d\u0438\u0435<\/p>\n<\/th>\n<th>\n<p align=\"left\">\u041a\u0430\u043d\u0430\u043b \u0442\u043e\u043b\u044c\u043a\u043e \u043d\u0430 \u0437\u0430\u043f\u0438\u0441\u044c<\/p>\n<\/th>\n<\/tr>\n<tr>\n<td>\n<p align=\"left\">\u0427\u0442\u0435\u043d\u0438\u0435<\/p>\n<\/td>\n<td>\n<p align=\"left\">\n<\/td>\n<td>\n<p align=\"left\">\u041e\u0448\u0438\u0431\u043a\u0430 \u043a\u043e\u043c\u043f\u0438\u043b\u044f\u0446\u0438\u0438<\/p>\n<\/td>\n<\/tr>\n<tr>\n<td>\n<p align=\"left\">\u0417\u0430\u043f\u0438\u0441\u044c<\/p>\n<\/td>\n<td>\n<p align=\"left\">\u041e\u0448\u0438\u0431\u043a\u0430 \u043a\u043e\u043c\u043f\u0438\u043b\u044f\u0446\u0438\u0438<\/p>\n<\/td>\n<td>\n<p align=\"left\">\n<\/td>\n<\/tr>\n<tr>\n<td>\n<p align=\"left\">\u0417\u0430\u043a\u0440\u044b\u0442\u0438\u0435<\/p>\n<\/td>\n<td>\n<p align=\"left\">\u041e\u0448\u0438\u0431\u043a\u0430 \u043a\u043e\u043c\u043f\u0438\u043b\u044f\u0446\u0438\u0438<\/p>\n<\/td>\n<td><\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<\/div>\n<\/div>\n<h2>\u0412\u0430\u0436\u043d\u044b\u0435 \u043f\u0440\u0430\u0432\u0438\u043b\u0430<\/h2>\n<ul>\n<li>\n<p>\u0417\u0430\u043a\u0440\u044b\u0432\u0430\u0435\u0442 \u043a\u0430\u043d\u0430\u043b \u0442\u043e\u0442, \u043a\u0442\u043e \u0432 \u043d\u0435\u0433\u043e \u043f\u0438\u0448\u0435\u0442.<\/p>\n<\/li>\n<li>\n<p>\u0415\u0441\u043b\u0438 \u043f\u0438\u0448\u0435\u0442 \u043d\u0435\u0441\u043a\u043e\u043b\u044c\u043a\u043e \u043f\u0440\u043e\u0434\u044e\u0441\u0435\u0440\u043e\u0432, \u0442\u043e \u0437\u0430\u043a\u0440\u044b\u0432\u0430\u0435\u0442 \u0442\u043e\u0442, \u043a\u0442\u043e \u0441\u043e\u0437\u0434\u0430\u043b \u043f\u0440\u043e\u0434\u044e\u0441\u0435\u0440\u043e\u0432.<\/p>\n<\/li>\n<li>\n<p>\u041d\u0435 \u0437\u0430\u043a\u0440\u044b\u0442\u044b\u0439 \u043a\u0430\u043d\u0430\u043b \u0434\u0435\u0440\u0436\u0438\u0442 \u0440\u0435\u0441\u0443\u0440\u0441\u044b. \u0417\u0430\u043a\u0440\u044b\u0432\u0430\u0442\u044c \u043d\u0430\u0434\u043e \u044f\u0432\u043d\u043e.<\/p>\n<\/li>\n<\/ul>\n<h2>\u041f\u0430\u0442\u0442\u0435\u0440\u043d\u044b<\/h2>\n<p><strong>Generator<\/strong> &#8212; \u043c\u0438\u043a\u0440\u043e\u043f\u0430\u0442\u0442\u0435\u0440\u043d, \u043a\u043e\u0442\u043e\u0440\u044b\u0439 \u043d\u0430\u043f\u043e\u043b\u043d\u044f\u0435\u0442 \u043a\u0430\u043d\u0430\u043b. \u0417\u0430\u043a\u0440\u044b\u0432\u0430\u0435\u043c \u043a\u0430\u043d\u0430\u043b, \u0447\u0442\u043e\u0431\u044b \u043d\u0435 \u0431\u044b\u043b\u043e \u043f\u0440\u043e\u0431\u043b\u0435\u043c.<\/p>\n<pre><code class=\"go\">func generator() &lt;- chan int { ch := make(chant int) go func(){ for i := 0; i &lt;= 12; i++ { ch &lt;- i + 1 } close(ch) }() return ch }<\/code><\/pre>\n<p><strong>Wrapper<\/strong> &#8212; \u043e\u0431\u043e\u0440\u0430\u0447\u0438\u0432\u0430\u0435\u043c \u0444\u0443\u043d\u043a\u0446\u0438\u044e, \u0434\u043e\u0431\u0430\u0432\u043b\u044f\u044f \u0444\u0443\u043d\u043a\u0446\u0438\u043e\u043d\u0430\u043b\u044c\u043d\u043e\u0441\u0442\u044c. \u0415\u0441\u043b\u0438 \u0432\u0430\u043c \u0447\u0442\u043e-\u0442\u043e \u0433\u043e\u0432\u043e\u0440\u0438\u0442 \u0441\u043b\u043e\u0432\u043e \u0434\u0435\u043a\u043e\u0440\u0430\u0442\u043e\u0440, \u0442\u043e \u044d\u0442\u043e \u0442\u043e\u0442 \u0441\u0430\u043c\u044b\u0439 \u043f\u0430\u0442\u0442\u0435\u0440\u043d.<\/p>\n<pre><code class=\"go\">func wrapper(wg *sync.WaitGroup, fn func()) {       wg.Add(1)          go func() {          defer wg.Done()             fmt.Println(\"Work before func\")          fn()          time.Sleep(1 * time.Second)          fmt.Println(\"Work after func\")       }()   }      func main() {       var wg sync.WaitGroup          wrapper(&amp;wg, func() {          time.Sleep(1 * time.Second)          fmt.Println(\"heavy work\")       })          wg.Wait()   }<\/code><\/pre>\n<p><strong>Fan-in <\/strong>&#8212; \u0441\u043e\u0431\u0438\u0440\u0430\u0435\u0442 \u0440\u0435\u0437\u0443\u043b\u044c\u0442\u0430\u0442\u044b \u0438\u0437 \u043d\u0435\u0441\u043a\u043e\u043b\u044c\u043a\u0438\u0445 \u043a\u0430\u043d\u0430\u043b\u043e\u0432 \u0432 \u043e\u0434\u0438\u043d.<\/p>\n<pre><code class=\"go\">func fanIn(input1, input2 &lt;-chan string) &lt;-chan string { ch := make(chan string) go func(){ for { select { case s := &lt;-input1: ch &lt;- s case s := &lt;-input2: ch &lt;- s } } }() return ch }<\/code><\/pre>\n<p><strong>Fan-out<\/strong> &#8212; \u043e\u0434\u043d\u0430 \u0438\u043b\u0438 \u043d\u0435\u0441\u043a\u043e\u043b\u044c\u043a\u043e \u0433\u043e\u0440\u0443\u0442\u0438\u043d \u043f\u0438\u0448\u0443\u0442 \u0432 \u043a\u0430\u043d\u0430\u043b, \u0441 \u0434\u0440\u0443\u0433\u043e\u0439 \u0441\u0442\u043e\u0440\u043e\u043d\u044b \u0440\u0430\u0431\u043e\u0447\u0438\u0435 \u0433\u043e\u0440\u0443\u0442\u0438\u043d\u044b \u0447\u0438\u0442\u0430\u044e\u0442 \u043a\u0430\u043d\u0430\u043b, \u0434\u0435\u043b\u0430\u044e\u0442 \u0440\u0430\u0431\u043e\u0442\u0443 \u0438 \u0443\u043c\u0438\u0440\u0430\u044e\u0442.<\/p>\n<pre><code class=\"go\">func worker(ch &lt;-chan int, wg *sync.WaitGroup) {       wg.Done()       for v := range ch {          fmt.Println(v)          time.Sleep(1 * time.Second)       }   }      func sender() {       ch := make(chan int)       var wg sync.WaitGroup       for i := 0; i &lt; 2; i++ {          wg.Add(1)          go worker(ch, &amp;wg)       }          for i := 0; i &lt; 10; i++ {          ch &lt;- i       }          close(ch)          wg.Wait()       fmt.Println(\"done\")   }<\/code><\/pre>\n<p><strong>Pipeline &#8212; <\/strong>\u0434\u0430\u043d\u043d\u044b\u0435 \u043e\u0431\u0440\u0430\u0431\u0430\u0442\u044b\u0432\u0430\u044e\u0442\u0441\u044f \u0446\u0435\u043f\u043e\u0447\u043a\u043e\u0439. Producer -&gt; Producer\/Consumer -&gt; Consumer. \u0421\u0442\u0430\u0434\u0438\u0439 \u043e\u0431\u0440\u0430\u0431\u043e\u0442\u043a\u0438 \u043c\u043e\u0436\u0435\u0442 \u0431\u044b\u0442\u044c \u0441\u043a\u043e\u043b\u044c\u043a\u043e \u0443\u0433\u043e\u0434\u043d\u043e.<\/p>\n<pre><code class=\"go\">func producer() &lt;-chan int {       c := make(chan int)          go func() {          for i := 0; i &lt;= 10; i++ {             c &lt;- i + 1          }          close(c)       }()          return c   }      func producerConsumer(c &lt;-chan int) &lt;-chan int {       out := make(chan int)          go func() {          for v := range c {             out &lt;- v * 2          }          close(out)       }()          return out   }      func consumer(ch &lt;-chan int) {       for v := range ch {          fmt.Println(v)       }   }<\/code><\/pre>\n<p><strong>Cancellation<\/strong> &#8212; \u0421\u043f\u043e\u0441\u043e\u0431 \u043f\u0440\u0435\u0440\u044b\u0432\u0430\u043d\u0438\u044f \u0433\u043e\u0440\u0443\u0442\u0438\u043d. \u041d\u0435\u043e\u0431\u0445\u043e\u0434\u0438\u043c, \u0447\u0442\u043e\u0431\u044b \u0438\u0437\u0431\u0435\u0433\u0430\u0442\u044c \u0432\u0438\u0441\u044f\u0449\u0438\u0445 \u0433\u043e\u0440\u0443\u0442\u0438\u043d, \u043e\u0441\u0442\u0430\u043d\u0430\u0432\u043b\u0438\u0432\u0430\u0442\u044c \u0441\u043b\u0438\u0448\u043a\u043e\u043c \u0434\u043e\u043b\u0433\u0438\u0435 \u043e\u043f\u0435\u0440\u0430\u0446\u0438\u0438.<\/p>\n<pre><code class=\"go\">\/\/ 1. WithCancel func worker(ctx context.Context) {       for {          select {          case &lt;-ctx.Done():             fmt.Println(\"Done\")             return          default:             fmt.Println(\"Working...\")             time.Sleep(500 * time.Millisecond)          }       }   }      func main() {       ctx, cancel := context.WithCancel(context.Background())       go worker(ctx)          time.Sleep(1 * time.Second) \/\/ \u0440\u0430\u0431\u043e\u0442\u0430\u0435\u043c       cancel()  \/\/ \u043e\u0442\u043c\u0435\u043d\u044f\u0435\u043c     time.Sleep(1 * time.Second)  \/\/ \u0432\u0440\u0435\u043c\u044f \u043d\u0430 \u0437\u0430\u0432\u0435\u0440\u0448\u0435\u043d\u0438\u0435 }  \/\/ 2. WithTimeout ctx, cancel := context.WithTimeout(context.Background(), 2*time.Second) \/\/ \u0441\u043f\u0443\u0441\u0442\u044f 2 \u0441\u0435\u043a\u0443\u043d\u0434\u044b \u0432\u043e\u0440\u043a\u0435\u0440 \u043f\u0435\u0440\u0435\u0441\u0442\u0430\u043d\u0435\u0442 \u0440\u0430\u0431\u043e\u0442\u0430\u0442\u044c   \/\/ 3. WithDeadline. \u041c\u043e\u0436\u043d\u043e \u0443\u043a\u0430\u0437\u0430\u0442\u044c \u0442\u043e\u0447\u043d\u043e\u0435 \u0432\u0440\u0435\u043c\u044f \u043e\u0441\u0442\u0430\u043d\u043e\u0432\u043a\u0438. ctx, cancel := context.WithDeadline(context.Background(), time.Now().Add(2*time.Second)) \/\/ \u043f\u0440\u0438\u0431\u0430\u0432\u043b\u044f\u0435\u043c \u043a \u0442\u0435\u043a\u0443\u0449\u0435\u043c\u0443 \u0432\u0440\u0435\u043c\u0435\u043d\u0438 \u0434\u0432\u0435 \u0441\u0435\u043a\u0443\u043d\u0434\u044b<\/code><\/pre>\n<p><strong>Worker pool <\/strong>&#8212; \u043a\u0430\u0436\u0434\u044b\u0439 \u0432\u043e\u0440\u043a\u0435\u0440 \u0431\u0435\u0440\u0451\u0442 \u0437\u0430\u0434\u0430\u0447\u0443, \u0434\u0435\u043b\u0430\u0435\u0442 \u0440\u0430\u0431\u043e\u0442\u0443 \u0438 \u043e\u0442\u043f\u0440\u0430\u0432\u043b\u044f\u0435\u0442 \u0440\u0435\u0437\u0443\u043b\u044c\u0442\u0430\u0442 \u0432 \u043a\u0430\u043d\u0430\u043b, \u0434\u0440\u0443\u0433\u0430\u044f \u0433\u043e\u0440\u0443\u0442\u0438\u043d\u0430, \u0432 \u043d\u0430\u0448\u0435\u043c \u0441\u043b\u0443\u0447\u0430\u0435 main, \u0447\u0438\u0442\u0430\u0435\u0442 \u0440\u0435\u0437\u0443\u043b\u044c\u0442\u0430\u0442 \u0438\u0437 \u043a\u0430\u043d\u0430\u043b\u0430.<\/p>\n<pre><code class=\"go\">func worker(jobs &lt;-chan int, results chan&lt;- int, wg *sync.WaitGroup) {       defer wg.Done()          for j := range jobs {          time.Sleep(1 * time.Second)          fmt.Println(\"job\", j)          results &lt;- j * j       }      }      func main() {       jobs := make(chan int)       results := make(chan int)          var wg sync.WaitGroup          for i := 0; i &lt; 3; i++ {          wg.Add(1)          go worker(jobs, results, &amp;wg)       }          go func() {          for i := 0; i &lt; 10; i++ {             jobs &lt;- i          }          close(jobs)       }()          go func() {          wg.Wait()          close(results)       }()          for result := range results {          fmt.Println(result)       }   }<\/code><\/pre>\n<\/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\/895922\/\"> https:\/\/habr.com\/ru\/articles\/895922\/<\/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<figure class=\"full-width\"><\/figure>\n<h2>\u0411\u0430\u0437\u0430<\/h2>\n<p>\u041f\u0430\u0440\u0430\u043b\u043b\u0435\u043b\u044c\u043d\u043e\u0441\u0442\u044c &#8212; \u0432\u044b\u043f\u043e\u043b\u043d\u0435\u043d\u0438\u0435 \u0437\u0430\u0434\u0430\u0447 \u0432 \u043e\u0434\u0438\u043d \u043c\u043e\u043c\u0435\u043d\u0442 \u0432\u0440\u0435\u043c\u0435\u043d\u0438 \u043d\u0430 \u0440\u0430\u0437\u043d\u044b\u0445 \u043b\u043e\u0433\u0438\u0447\u0435\u0441\u043a\u0438\u0445 \u044f\u0434\u0440\u0430\u0445.<br \/>\u041a\u043e\u043d\u043a\u0443\u0440\u0435\u043d\u0442\u043d\u043e\u0441\u0442\u044c &#8212; \u0432\u044b\u043f\u043e\u043b\u043d\u0435\u043d\u0438\u0435 \u0437\u0430\u0434\u0430\u0447 \u043f\u043e\u0441\u043b\u0435\u0434\u043e\u0432\u0430\u0442\u0435\u043b\u044c\u043d\u043e, \u043d\u043e \u0441\u043e \u0441\u043c\u0435\u043d\u043e\u0439 \u043a\u043e\u043d\u0442\u0435\u043a\u0441\u0442\u0430 \u043d\u0430 \u0434\u0440\u0443\u0433\u0443\u044e \u0437\u0430\u0434\u0430\u0447\u0443 \u0432 \u043e\u0436\u0438\u0434\u0430\u043d\u0438\u0435 \u0437\u0430\u0432\u0435\u0440\u0448\u0435\u043d\u0438\u044f \u0438\u043d\u043e\u0439 \u0437\u0430\u0434\u0430\u0447\u0438. \u0423 \u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u0435\u043b\u044f \u043c\u043e\u0436\u0435\u0442 \u0432\u043e\u0437\u043d\u0438\u043a\u043d\u0443\u0442\u044c \u0438\u043b\u043b\u044e\u0437\u0438\u044f \u043c\u043d\u043e\u0433\u043e\u0437\u0430\u0434\u0430\u0447\u043d\u043e\u0441\u0442\u0438 \u0434\u0430\u0436\u0435 \u0432 \u043e\u0434\u043d\u043e\u043f\u0440\u043e\u0446\u0435\u0441\u0441\u043e\u0440\u043d\u043e\u0439 \u0441\u0438\u0441\u0442\u0435\u043c\u0435, \u043f\u043e\u0441\u043a\u043e\u043b\u044c\u043a\u0443 \u0441\u043c\u0435\u043d\u0430 \u043a\u043e\u043d\u0442\u0435\u043a\u0441\u0442\u0430 \u043f\u0440\u043e\u0438\u0441\u0445\u043e\u0434\u0438\u0442 \u0431\u044b\u0441\u0442\u0440\u043e (\u043c\u0438\u043a\u0440\u043e\u0441\u0435\u043a\u0443\u043d\u0434\u044b).<\/p>\n<p>\u041f\u0440\u043e\u0446\u0435\u0441\u0441\u044b:<\/p>\n<ul>\n<li>\n<p>\u0420\u0430\u0437\u0434\u0435\u043b\u044c\u043d\u0430\u044f \u043f\u0430\u043c\u044f\u0442\u044c<\/p>\n<\/li>\n<li>\n<p>\u0420\u0430\u0437\u0434\u0435\u043b\u044c\u043d\u044b\u0435 \u0440\u0435\u0441\u0443\u0440\u0441\u044b<\/p>\n<\/li>\n<li>\n<p>\u0420\u0430\u0437\u0434\u0435\u043b\u044c\u043d\u044b\u0435 \u0440\u0435\u0433\u0438\u0441\u0442\u0440\u044b<\/p>\n<\/li>\n<\/ul>\n<p>\u041f\u043e\u0442\u043e\u043a\u0438:<\/p>\n<ul>\n<li>\n<p>\u041e\u0431\u0449\u0430\u044f \u043f\u0430\u043c\u044f\u0442\u044c<\/p>\n<\/li>\n<li>\n<p>\u041e\u0431\u0449\u0438\u0435 \u0440\u0435\u0441\u0443\u0440\u0441\u044b<\/p>\n<\/li>\n<li>\n<p>\u0420\u0430\u0437\u0434\u0435\u043b\u044c\u043d\u044b\u0435 \u0441\u0442\u044d\u043a \u0438 \u0440\u0435\u0433\u0438\u0441\u0442\u0440\u044b<\/p>\n<\/li>\n<\/ul>\n<p>\u0413\u043e\u0440\u0443\u0442\u0438\u043d\u044b:<\/p>\n<ul>\n<li>\n<p>\u041e\u0431\u0449\u0430\u044f \u043f\u0430\u043c\u044f\u0442\u044c<\/p>\n<\/li>\n<li>\n<p>\u041e\u0431\u0449\u0438\u0435 \u0440\u0435\u0441\u0443\u0440\u0441\u044b<\/p>\n<\/li>\n<li>\n<p>\u041e\u0431\u0449\u0438\u0439 \u0441\u0438\u0441\u0442\u0435\u043c\u043d\u044b\u0439 \u0441\u0442\u044d\u043a<\/p>\n<\/li>\n<li>\n<p>\u041e\u0431\u0449\u0438\u0435 \u0440\u0435\u0433\u0438\u0441\u0442\u0440\u044b<\/p>\n<\/li>\n<\/ul>\n<p>Go runtime \u043f\u0440\u0435\u0434\u0441\u0442\u0430\u0432\u043b\u044f\u0435\u0442 \u043c\u043e\u0434\u0435\u043b\u044c P:M:G.<br \/> P &#8212; \u043f\u0440\u0435\u0434\u0441\u0442\u0430\u0432\u043b\u044f\u0435\u0442 \u043b\u043e\u0433\u0438\u0447\u0435\u0441\u043a\u043e\u0435 \u044f\u0434\u0440\u043e \u043f\u0440\u043e\u0446\u0435\u0441\u0441\u043e\u0440\u0430.<br \/> M &#8212; \u043f\u043e\u0442\u043e\u043a \u041e\u0421 \u043f\u043e \u0447\u0438\u0441\u043b\u0443 \u043f\u0440\u043e\u0446\u0435\u0441\u0441\u043e\u0440\u043e\u0432 P.<br \/> G &#8212; \u0441\u0442\u0440\u0443\u043a\u0442\u0443\u0440\u0430, \u043a\u043e\u0442\u043e\u0440\u0430\u044f \u0432\u044b\u043f\u043e\u043b\u043d\u044f\u0435\u0442 \u043f\u0435\u0440\u0435\u0434\u0430\u043d\u043d\u0443\u044e \u0444\u0443\u043d\u043a\u0446\u0438\u044e, \u0441\u043e\u0437\u0434\u0430\u0451\u0442\u0441\u044f \u043f\u043e \u043d\u0435\u043e\u0431\u0445\u043e\u0434\u0438\u043c\u043e\u0441\u0442\u0438, \u043c\u0438\u043d\u0438\u043c\u0443\u043c \u043e\u0434\u043d\u0430 \u043d\u0430 \u0441\u0442\u0430\u0440\u0442\u0435 \u043f\u0440\u043e\u0433\u0440\u0430\u043c\u043c\u044b (main). \u0421\u0442\u044d\u043a \u0432\u0441\u0435\u0433\u043e 2\u043a\u0431, \u043c\u043e\u0436\u0435\u0442 \u0440\u0430\u0441\u0448\u0438\u0440\u044f\u0442\u0441\u044f \u0434\u043e 1\u0433\u0431 \u0434\u043b\u044f 64x \u0438 \u0434\u043e 250\u043a\u0431 \u0434\u043b\u044f 32\u0445 \u0441\u0438\u0441\u0442\u0435\u043c.<\/p>\n<p>\u0423\u043f\u0440\u0430\u0432\u043b\u0435\u043d\u0438\u0435 \u0433\u043e\u0440\u0443\u0442\u0438\u043d\u0430\u043c\u0438 \u043e\u0441\u0443\u0449\u0435\u0441\u0442\u0432\u043b\u044f\u0435\u0442\u0441\u044f \u043f\u043b\u0430\u043d\u0438\u0440\u043e\u0432\u0449\u0438\u043a\u043e\u043c Go, \u0430 \u043d\u0435 \u041e\u0421. \u041f\u043b\u0430\u043d\u0438\u0440\u043e\u0432\u0449\u0438\u043a Go \u0440\u0430\u0431\u043e\u0442\u0430\u0435\u0442 \u0432 \u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u0435\u043b\u044c\u0441\u043a\u043e\u043c \u043f\u0440\u043e\u0441\u0442\u0440\u0430\u043d\u0441\u0442\u0432\u0435. \u041c\u044b \u043d\u0435 \u043c\u043e\u0436\u0435\u043c \u043d\u0430\u043f\u0440\u044f\u043c\u0443\u044e \u0443\u043f\u0440\u0430\u0432\u043b\u044f\u0442\u044c \u043d\u0430 \u043a\u0430\u043a\u043e\u043c \u043f\u0440\u043e\u0446\u0435\u0441\u0441\u043e\u0440\u0435 \u0431\u0443\u0434\u0435\u0442 \u0438\u0441\u043f\u043e\u043b\u043d\u044f\u0442\u044c\u0441\u044f \u0433\u043e\u0440\u0443\u0442\u0438\u043d\u0430, \u0437\u0430 \u044d\u0442\u043e \u043e\u0442\u0432\u0435\u0447\u0430\u0435\u0442 \u043f\u043b\u0430\u043d\u0438\u0440\u043e\u0432\u0449\u0438\u043a.<\/p>\n<p>\u041a\u0430\u043d\u0430\u043b &#8212; \u043e\u0447\u0435\u0440\u0435\u0434\u044c \u0441\u043e\u043e\u0431\u0449\u0435\u043d\u0438\u0439, \u043a\u043e\u0442\u043e\u0440\u0430\u044f \u0443\u043c\u0435\u0435\u0442 \u0440\u0430\u0431\u043e\u0442\u0430\u0442\u044c \u0432 \u043c\u043d\u043e\u0433\u043e\u043f\u043e\u0442\u043e\u0447\u043d\u043e\u0439 \u0441\u0440\u0435\u0434\u0435, \u0440\u0430\u0431\u043e\u0442\u0430\u0435\u0442 \u043f\u043e \u043f\u0440\u0438\u043d\u0446\u0438\u043f\u0443 FIFO.<br \/>\u0415\u0441\u0442\u044c \u0434\u0432\u0430 \u0442\u0438\u043f\u0430 \u043a\u0430\u043d\u0430\u043b\u043e\u0432: \u0431\u0443\u0444\u0435\u0440\u0438\u0437\u043e\u0432\u0430\u043d\u043d\u044b\u0439 \u0438 \u043d\u0435\u0431\u0443\u0444\u0435\u0440\u0438\u0437\u043e\u0432\u0430\u043d\u043d\u044b\u0439.<br \/>\u041f\u0435\u0440\u0432\u044b\u0439 \u043c\u043e\u0436\u0435\u0442 \u0445\u0440\u0430\u043d\u0438\u0442\u044c \u043d\u0435\u0441\u043a\u043e\u043b\u044c\u043a\u043e \u0441\u043e\u043e\u0431\u0449\u0435\u043d\u0438\u0439, \u0432\u0442\u043e\u0440\u043e\u0439 \u0442\u043e\u043b\u044c\u043a\u043e \u043e\u0434\u043d\u043e.<\/p>\n<h2>\u0421\u0438\u043d\u0445\u0440\u043e\u043d\u0438\u0437\u0430\u0446\u0438\u044f<\/h2>\n<p>sync.WaitGroup &#8212; \u0441\u0447\u0451\u0442\u0447\u0438\u043a, \u043a\u043e\u0442\u043e\u0440\u044b\u0439 \u043f\u043e\u0437\u0432\u043e\u043b\u044f\u0435\u0442 \u043f\u043e\u0434\u043e\u0436\u0434\u0430\u0442\u044c \u0437\u0430\u0432\u0435\u0440\u0448\u0435\u043d\u0438\u044f \u0433\u043e\u0440\u0443\u0442\u0438\u043d.<br \/>sync.Mutex &#8212; \u0431\u043b\u043e\u043a\u0438\u0440\u0443\u0435\u0442 \u0434\u043e\u0441\u0442\u0443\u043f \u043a \u0440\u0435\u0441\u0443\u0440\u0441\u0443.<br \/>sync.RwMutex &#8212; \u0440\u0430\u0437\u0434\u0435\u043b\u044f\u0435\u043c\u0430\u044f \u0431\u043b\u043e\u043a\u0438\u0440\u043e\u0432\u043a\u0430 \u043d\u0430 \u0447\u0442\u0435\u043d\u0438\u0435 \u0438 \u0437\u0430\u043f\u0438\u0441\u044c. \u0427\u0438\u0442\u0430\u0442\u044c \u043c\u043e\u0433\u0443\u0442 \u043d\u0435\u0441\u043a\u043e\u043b\u044c\u043a\u043e \u0433\u043e\u0440\u0443\u0442\u0438\u043d, \u043d\u043e \u043c\u0443\u0442\u0438\u0440\u043e\u0432\u0430\u0442\u044c \u0434\u0430\u043d\u043d\u044b\u0435 \u0442\u043e\u043b\u044c\u043a\u043e \u043e\u0434\u043d\u0430.<br \/>sync.Atomic &#8212; \u0430\u0442\u043e\u043c\u0430\u0440\u043d\u0430\u044f \u043e\u043f\u0435\u0440\u0430\u0446\u0438\u044f \u0447\u0442\u0435\u043d\u0438\u044f \u0438 \u0437\u0430\u043f\u0438\u0441\u0438. \u0420\u0430\u0431\u043e\u0442\u0430\u0435\u0442 \u0442\u043e\u043b\u044c\u043a\u043e \u0441 \u043f\u0440\u043e\u0441\u0442\u044b\u043c\u0438 \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u044f\u043c\u0438.<br \/>sync.Map &#8212; lock-free \u0441\u0442\u0440\u0443\u043a\u0442\u0443\u0440\u0430. \u0420\u0430\u0431\u043e\u0442\u0430\u0435\u0442 \u0442\u0430\u043a \u0436\u0435, \u043a\u0430\u043a \u0438 \u043e\u0431\u044b\u0447\u043d\u0430\u044f map, \u043d\u043e \u043f\u043e\u0442\u043e\u043a\u043e\u0431\u0435\u0437\u043e\u043f\u0430\u0441\u043d\u0430\u044f, \u043c\u043e\u0436\u043d\u043e \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c \u0432 \u043c\u043d\u043e\u0433\u043e\u043f\u043e\u0442\u043e\u0447\u043d\u043e\u0439 \u0441\u0440\u0435\u0434\u0435. \u0425\u043e\u0440\u043e\u0448\u043e \u043f\u043e\u0434\u0445\u043e\u0434\u0438\u0442 \u0434\u043b\u044f \u0441\u043b\u0443\u0447\u0430\u0435\u0432, \u0433\u0434\u0435 \u043d\u0430\u0434\u043e \u043c\u043d\u043e\u0433\u043e \u0447\u0438\u0442\u0430\u0442\u044c \u0438 \u043c\u0430\u043b\u043e \u043f\u0438\u0441\u0430\u0442\u044c. \u0415\u0441\u043b\u0438 \u043d\u0430\u0434\u043e \u043c\u043d\u043e\u0433\u043e \u043f\u0438\u0441\u0430\u0442\u044c, \u0442\u043e \u043b\u0443\u0447\u0448\u0435 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c \u043e\u0431\u044b\u0447\u043d\u0443\u044e map \u0438 sync.RwMutex.<\/p>\n<h2>\u041d\u0435\u0431\u0443\u0444\u0435\u0440\u0438\u0437\u043e\u0432\u0430\u043d\u043d\u044b\u0439 \u043a\u0430\u043d\u0430\u043b<\/h2>\n<div>\n<div class=\"table\">\n<table>\n<tbody>\n<tr>\n<th>\n<p align=\"left\">\u0414\u0435\u0439\u0441\u0442\u0432\u0438\u0435<\/p>\n<\/th>\n<th>\n<p align=\"left\">\u041e\u0442\u043a\u0440\u044b\u0442\u044b\u0439 \u043a\u0430\u043d\u0430\u043b<\/p>\n<\/th>\n<th>\n<p align=\"left\">\u0417\u0430\u043a\u0440\u044b\u0442\u044b\u0439 \u043a\u0430\u043d\u0430\u043b<\/p>\n<\/th>\n<th>\n<p align=\"left\">\u041d\u0435\u0438\u043d\u0438\u0446\u0438\u0430\u043b\u0438\u0437\u0438\u0440\u043e\u0432\u0430\u043d\u043d\u044b\u0439 \u043a\u0430\u043d\u0430\u043b<\/p>\n<\/th>\n<\/tr>\n<tr>\n<td>\n<p align=\"left\">\u0427\u0442\u0435\u043d\u0438\u0435<\/p>\n<\/td>\n<td>\n<p align=\"left\">\u0411\u043b\u043e\u043a\u0438\u0440\u043e\u0432\u043a\u0430 \u0434\u043e \u043f\u0440\u0438\u0445\u043e\u0434\u0430 \u043f\u0438\u0441\u0430\u0442\u0435\u043b\u044f<\/p>\n<\/td>\n<td>\n<p align=\"left\">Zero value<\/p>\n<\/td>\n<td>\n<p align=\"left\">\u0411\u043b\u043e\u043a\u0438\u0440\u043e\u0432\u043a\u0430 \u043d\u0430\u0432\u0441\u0435\u0433\u0434\u0430<\/p>\n<\/td>\n<\/tr>\n<tr>\n<td>\n<p align=\"left\">\u0417\u0430\u043f\u0438\u0441\u044c<\/p>\n<\/td>\n<td>\n<p align=\"left\">\u0411\u043b\u043e\u043a\u0438\u0440\u043e\u0432\u043a\u0430 \u0434\u043e \u043f\u0440\u0438\u0445\u043e\u0434\u0430 \u0447\u0438\u0442\u0430\u0442\u0435\u043b\u044f<\/p>\n<\/td>\n<td>\n<p align=\"left\">Panic<\/p>\n<\/td>\n<td>\n<p align=\"left\">\u0411\u043b\u043e\u043a\u0438\u0440\u043e\u0432\u043a\u0430 \u043d\u0430\u0432\u0441\u0435\u0433\u0434\u0430<\/p>\n<\/td>\n<\/tr>\n<tr>\n<td>\n<p align=\"left\">\u0417\u0430\u043a\u0440\u044b\u0442\u0438\u0435<\/p>\n<\/td>\n<td>\n<p align=\"left\">\u041a\u0430\u043d\u0430\u043b \u0437\u0430\u043a\u0440\u043e\u0435\u0442\u0441\u044f<\/p>\n<\/td>\n<td>\n<p align=\"left\">Panic<\/p>\n<\/td>\n<td>\n<p align=\"left\">Panic<\/p>\n<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<\/div>\n<\/div>\n<h2>\u0411\u0443\u0444\u0435\u0440\u0438\u0437\u043e\u0432\u0430\u043d\u043d\u044b\u0439 \u043a\u0430\u043d\u0430\u043b<\/h2>\n<div>\n<div class=\"table\">\n<table>\n<tbody>\n<tr>\n<th>\n<p align=\"left\">\u0414\u0435\u0439\u0441\u0442\u0432\u0438\u0435<\/p>\n<\/th>\n<th>\n<p align=\"left\">\u041e\u0442\u043a\u0440\u044b\u0442\u044b\u0439 \u0438 \u0447\u0430\u0441\u0442\u0438\u0447\u043d\u043e \u0437\u0430\u043f\u043e\u043b\u043d\u0435\u043d\u043d\u044b\u0439<\/p>\n<\/th>\n<th>\n<p align=\"left\">\u041e\u0442\u043a\u0440\u044b\u0442\u044b\u0439 \u0438 \u043f\u043e\u043b\u043d\u043e\u0441\u0442\u044c\u044e \u0437\u0430\u043f\u043e\u043b\u043d\u0435\u043d\u043d\u044b\u0439<\/p>\n<\/th>\n<th>\n<p align=\"left\">\u041e\u0442\u043a\u0440\u044b\u0442\u044b\u0439 \u0438 \u043f\u0443\u0441\u0442\u043e\u0439<\/p>\n<\/th>\n<th>\n<p align=\"left\">\u0417\u0430\u043a\u0440\u044b\u0442\u044b\u0439 \u0438 \u0447\u0430\u0441\u0442\u0438\u0447\u043d\u043e \u0437\u0430\u043f\u043e\u043b\u043d\u0435\u043d\u043d\u044b\u0439<\/p>\n<\/th>\n<\/tr>\n<tr>\n<td>\n<p align=\"left\">\u0427\u0442\u0435\u043d\u0438\u0435<\/p>\n<\/td>\n<td>\n<p align=\"left\">\u041f\u0440\u043e\u0447\u0438\u0442\u0430\u0435\u043c \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0435<\/p>\n<\/td>\n<td>\n<p align=\"left\">\u041f\u0440\u043e\u0447\u0438\u0442\u0430\u0435\u043c \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0435<\/p>\n<\/td>\n<td>\n<p align=\"left\">\u0411\u043b\u043e\u043a\u0438\u0440\u043e\u0432\u043a\u0430 \u0434\u043e \u043f\u0440\u0438\u0445\u043e\u0434\u0430 \u043f\u0438\u0441\u0430\u0442\u0435\u043b\u044f<\/p>\n<\/td>\n<td>\n<p align=\"left\">\u041f\u0440\u043e\u0447\u0438\u0442\u0430\u0435\u043c \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0435<\/p>\n<\/td>\n<\/tr>\n<tr>\n<td>\n<p align=\"left\">\u0417\u0430\u043f\u0438\u0441\u044c<\/p>\n<\/td>\n<td>\n<p align=\"left\">\u0417\u0430\u043f\u0438\u0448\u0435\u043c \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0435<\/p>\n<\/td>\n<td>\n<p align=\"left\">\u0411\u043b\u043e\u043a\u0438\u0440\u043e\u0432\u043a\u0430 \u0434\u043e \u043f\u0440\u0438\u0445\u043e\u0434\u0430 \u0447\u0438\u0442\u0430\u0442\u0435\u043b\u044f<\/p>\n<\/td>\n<td>\n<p align=\"left\">\u0417\u0430\u043f\u0438\u0448\u0435\u043c \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0435<\/p>\n<\/td>\n<td>\n<p align=\"left\">Panic<\/p>\n<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<\/div>\n<\/div>\n<h2>\u041e\u0433\u0440\u0430\u043d\u0438\u0447\u0435\u043d\u0438\u044f \u043a\u0430\u043d\u0430\u043b\u0430<\/h2>\n<div>\n<div class=\"table\">\n<table>\n<tbody>\n<tr>\n<th>\n<p align=\"left\">\u0414\u0435\u0439\u0441\u0442\u0432\u0438\u0435<\/p>\n<\/th>\n<th>\n<p align=\"left\">\u041a\u0430\u043d\u0430\u043b \u0442\u043e\u043b\u044c\u043a\u043e \u043d\u0430 \u0447\u0442\u0435\u043d\u0438\u0435<\/p>\n<\/th>\n<th>\n<p align=\"left\">\u041a\u0430\u043d\u0430\u043b \u0442\u043e\u043b\u044c\u043a\u043e \u043d\u0430 \u0437\u0430\u043f\u0438\u0441\u044c<\/p>\n<\/th>\n<\/tr>\n<tr>\n<td>\n<p align=\"left\">\u0427\u0442\u0435\u043d\u0438\u0435<\/p>\n<\/td>\n<td>\n<p align=\"left\">\n<\/td>\n<td>\n<p align=\"left\">\u041e\u0448\u0438\u0431\u043a\u0430 \u043a\u043e\u043c\u043f\u0438\u043b\u044f\u0446\u0438\u0438<\/p>\n<\/td>\n<\/tr>\n<tr>\n<td>\n<p align=\"left\">\u0417\u0430\u043f\u0438\u0441\u044c<\/p>\n<\/td>\n<td>\n<p align=\"left\">\u041e\u0448\u0438\u0431\u043a\u0430 \u043a\u043e\u043c\u043f\u0438\u043b\u044f\u0446\u0438\u0438<\/p>\n<\/td>\n<td>\n<p align=\"left\">\n<\/td>\n<\/tr>\n<tr>\n<td>\n<p align=\"left\">\u0417\u0430\u043a\u0440\u044b\u0442\u0438\u0435<\/p>\n<\/td>\n<td>\n<p align=\"left\">\u041e\u0448\u0438\u0431\u043a\u0430 \u043a\u043e\u043c\u043f\u0438\u043b\u044f\u0446\u0438\u0438<\/p>\n<\/td>\n<td><\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<\/div>\n<\/div>\n<h2>\u0412\u0430\u0436\u043d\u044b\u0435 \u043f\u0440\u0430\u0432\u0438\u043b\u0430<\/h2>\n<ul>\n<li>\n<p>\u0417\u0430\u043a\u0440\u044b\u0432\u0430\u0435\u0442 \u043a\u0430\u043d\u0430\u043b \u0442\u043e\u0442, \u043a\u0442\u043e \u0432 \u043d\u0435\u0433\u043e \u043f\u0438\u0448\u0435\u0442.<\/p>\n<\/li>\n<li>\n<p>\u0415\u0441\u043b\u0438 \u043f\u0438\u0448\u0435\u0442 \u043d\u0435\u0441\u043a\u043e\u043b\u044c\u043a\u043e \u043f\u0440\u043e\u0434\u044e\u0441\u0435\u0440\u043e\u0432, \u0442\u043e \u0437\u0430\u043a\u0440\u044b\u0432\u0430\u0435\u0442 \u0442\u043e\u0442, \u043a\u0442\u043e \u0441\u043e\u0437\u0434\u0430\u043b \u043f\u0440\u043e\u0434\u044e\u0441\u0435\u0440\u043e\u0432.<\/p>\n<\/li>\n<li>\n<p>\u041d\u0435 \u0437\u0430\u043a\u0440\u044b\u0442\u044b\u0439 \u043a\u0430\u043d\u0430\u043b \u0434\u0435\u0440\u0436\u0438\u0442 \u0440\u0435\u0441\u0443\u0440\u0441\u044b. \u0417\u0430\u043a\u0440\u044b\u0432\u0430\u0442\u044c \u043d\u0430\u0434\u043e \u044f\u0432\u043d\u043e.<\/p>\n<\/li>\n<\/ul>\n<h2>\u041f\u0430\u0442\u0442\u0435\u0440\u043d\u044b<\/h2>\n<p><strong>Generator<\/strong> &#8212; \u043c\u0438\u043a\u0440\u043e\u043f\u0430\u0442\u0442\u0435\u0440\u043d, \u043a\u043e\u0442\u043e\u0440\u044b\u0439 \u043d\u0430\u043f\u043e\u043b\u043d\u044f\u0435\u0442 \u043a\u0430\u043d\u0430\u043b. \u0417\u0430\u043a\u0440\u044b\u0432\u0430\u0435\u043c \u043a\u0430\u043d\u0430\u043b, \u0447\u0442\u043e\u0431\u044b \u043d\u0435 \u0431\u044b\u043b\u043e \u043f\u0440\u043e\u0431\u043b\u0435\u043c.<\/p>\n<pre><code class=\"go\">func generator() &lt;- chan int { ch := make(chant int) go func(){ for i := 0; i &lt;= 12; i++ { ch &lt;- i + 1 } close(ch) }() return ch }<\/code><\/pre>\n<p><strong>Wrapper<\/strong> &#8212; \u043e\u0431\u043e\u0440\u0430\u0447\u0438\u0432\u0430\u0435\u043c \u0444\u0443\u043d\u043a\u0446\u0438\u044e, \u0434\u043e\u0431\u0430\u0432\u043b\u044f\u044f \u0444\u0443\u043d\u043a\u0446\u0438\u043e\u043d\u0430\u043b\u044c\u043d\u043e\u0441\u0442\u044c. \u0415\u0441\u043b\u0438 \u0432\u0430\u043c \u0447\u0442\u043e-\u0442\u043e \u0433\u043e\u0432\u043e\u0440\u0438\u0442 \u0441\u043b\u043e\u0432\u043e \u0434\u0435\u043a\u043e\u0440\u0430\u0442\u043e\u0440, \u0442\u043e \u044d\u0442\u043e \u0442\u043e\u0442 \u0441\u0430\u043c\u044b\u0439 \u043f\u0430\u0442\u0442\u0435\u0440\u043d.<\/p>\n<pre><code class=\"go\">func wrapper(wg *sync.WaitGroup, fn func()) {       wg.Add(1)          go func() {          defer wg.Done()             fmt.Println(\"Work before func\")          fn()          time.Sleep(1 * time.Second)          fmt.Println(\"Work after func\")       }()   }      func main() {       var wg sync.WaitGroup          wrapper(&amp;wg, func() {          time.Sleep(1 * time.Second)          fmt.Println(\"heavy work\")       })          wg.Wait()   }<\/code><\/pre>\n<p><strong>Fan-in <\/strong>&#8212; \u0441\u043e\u0431\u0438\u0440\u0430\u0435\u0442 \u0440\u0435\u0437\u0443\u043b\u044c\u0442\u0430\u0442\u044b \u0438\u0437 \u043d\u0435\u0441\u043a\u043e\u043b\u044c\u043a\u0438\u0445 \u043a\u0430\u043d\u0430\u043b\u043e\u0432 \u0432 \u043e\u0434\u0438\u043d.<\/p>\n<pre><code class=\"go\">func fanIn(input1, input2 &lt;-chan string) &lt;-chan string { ch := make(chan string) go func(){ for { select { case s := &lt;-input1: ch &lt;- s case s := &lt;-input2: ch &lt;- s } } }() return ch }<\/code><\/pre>\n<p><strong>Fan-out<\/strong> &#8212; \u043e\u0434\u043d\u0430 \u0438\u043b\u0438 \u043d\u0435\u0441\u043a\u043e\u043b\u044c\u043a\u043e \u0433\u043e\u0440\u0443\u0442\u0438\u043d \u043f\u0438\u0448\u0443\u0442 \u0432 \u043a\u0430\u043d\u0430\u043b, \u0441 \u0434\u0440\u0443\u0433\u043e\u0439 \u0441\u0442\u043e\u0440\u043e\u043d\u044b \u0440\u0430\u0431\u043e\u0447\u0438\u0435 \u0433\u043e\u0440\u0443\u0442\u0438\u043d\u044b \u0447\u0438\u0442\u0430\u044e\u0442 \u043a\u0430\u043d\u0430\u043b, \u0434\u0435\u043b\u0430\u044e\u0442 \u0440\u0430\u0431\u043e\u0442\u0443 \u0438 \u0443\u043c\u0438\u0440\u0430\u044e\u0442.<\/p>\n<pre><code class=\"go\">func worker(ch &lt;-chan int, wg *sync.WaitGroup) {       wg.Done()       for v := range ch {          fmt.Println(v)          time.Sleep(1 * time.Second)       }   }      func sender() {       ch := make(chan int)       var wg sync.WaitGroup       for i := 0; i &lt; 2; i++ {          wg.Add(1)          go worker(ch, &amp;wg)       }          for i := 0; i &lt; 10; i++ {          ch &lt;- i       }          close(ch)          wg.Wait()       fmt.Println(\"done\")   }<\/code><\/pre>\n<p><strong>Pipeline &#8212; <\/strong>\u0434\u0430\u043d\u043d\u044b\u0435 \u043e\u0431\u0440\u0430\u0431\u0430\u0442\u044b\u0432\u0430\u044e\u0442\u0441\u044f \u0446\u0435\u043f\u043e\u0447\u043a\u043e\u0439. Producer -&gt; Producer\/Consumer -&gt; Consumer. \u0421\u0442\u0430\u0434\u0438\u0439 \u043e\u0431\u0440\u0430\u0431\u043e\u0442\u043a\u0438 \u043c\u043e\u0436\u0435\u0442 \u0431\u044b\u0442\u044c \u0441\u043a\u043e\u043b\u044c\u043a\u043e \u0443\u0433\u043e\u0434\u043d\u043e.<\/p>\n<pre><code class=\"go\">func producer() &lt;-chan int {       c := make(chan int)          go func() {          for i := 0; i &lt;= 10; i++ {             c &lt;- i + 1          }          close(c)       }()          return c   }      func producerConsumer(c &lt;-chan int) &lt;-chan int {       out := make(chan int)          go func() {          for v := range c {             out &lt;- v * 2          }          close(out)       }()          return out   }      func consumer(ch &lt;-chan int) {       for v := range ch {          fmt.Println(v)       }   }<\/code><\/pre>\n<p><strong>Cancellation<\/strong> &#8212; \u0421\u043f\u043e\u0441\u043e\u0431 \u043f\u0440\u0435\u0440\u044b\u0432\u0430\u043d\u0438\u044f \u0433\u043e\u0440\u0443\u0442\u0438\u043d. \u041d\u0435\u043e\u0431\u0445\u043e\u0434\u0438\u043c, \u0447\u0442\u043e\u0431\u044b \u0438\u0437\u0431\u0435\u0433\u0430\u0442\u044c \u0432\u0438\u0441\u044f\u0449\u0438\u0445 \u0433\u043e\u0440\u0443\u0442\u0438\u043d, \u043e\u0441\u0442\u0430\u043d\u0430\u0432\u043b\u0438\u0432\u0430\u0442\u044c \u0441\u043b\u0438\u0448\u043a\u043e\u043c \u0434\u043e\u043b\u0433\u0438\u0435 \u043e\u043f\u0435\u0440\u0430\u0446\u0438\u0438.<\/p>\n<pre><code class=\"go\">\/\/ 1. WithCancel func worker(ctx context.Context) {       for {          select {          case &lt;-ctx.Done():             fmt.Println(\"Done\")             return          default:             fmt.Println(\"Working...\")             time.Sleep(500 * time.Millisecond)          }       }   }      func main() {       ctx, cancel := context.WithCancel(context.Background())       go worker(ctx)          time.Sleep(1 * time.Second) \/\/ \u0440\u0430\u0431\u043e\u0442\u0430\u0435\u043c       cancel()  \/\/ \u043e\u0442\u043c\u0435\u043d\u044f\u0435\u043c     time.Sleep(1 * time.Second)  \/\/ \u0432\u0440\u0435\u043c\u044f \u043d\u0430 \u0437\u0430\u0432\u0435\u0440\u0448\u0435\u043d\u0438\u0435 }  \/\/ 2. WithTimeout ctx, cancel := context.WithTimeout(context.Background(), 2*time.Second) \/\/ \u0441\u043f\u0443\u0441\u0442\u044f 2 \u0441\u0435\u043a\u0443\u043d\u0434\u044b \u0432\u043e\u0440\u043a\u0435\u0440 \u043f\u0435\u0440\u0435\u0441\u0442\u0430\u043d\u0435\u0442 \u0440\u0430\u0431\u043e\u0442\u0430\u0442\u044c   \/\/ 3. WithDeadline. \u041c\u043e\u0436\u043d\u043e \u0443\u043a\u0430\u0437\u0430\u0442\u044c \u0442\u043e\u0447\u043d\u043e\u0435 \u0432\u0440\u0435\u043c\u044f \u043e\u0441\u0442\u0430\u043d\u043e\u0432\u043a\u0438. ctx, cancel := context.WithDeadline(context.Background(), time.Now().Add(2*time.Second)) \/\/ \u043f\u0440\u0438\u0431\u0430\u0432\u043b\u044f\u0435\u043c \u043a \u0442\u0435\u043a\u0443\u0449\u0435\u043c\u0443 \u0432\u0440\u0435\u043c\u0435\u043d\u0438 \u0434\u0432\u0435 \u0441\u0435\u043a\u0443\u043d\u0434\u044b<\/code><\/pre>\n<p><strong>Worker pool <\/strong>&#8212; \u043a\u0430\u0436\u0434\u044b\u0439 \u0432\u043e\u0440\u043a\u0435\u0440 \u0431\u0435\u0440\u0451\u0442 \u0437\u0430\u0434\u0430\u0447\u0443, \u0434\u0435\u043b\u0430\u0435\u0442 \u0440\u0430\u0431\u043e\u0442\u0443 \u0438 \u043e\u0442\u043f\u0440\u0430\u0432\u043b\u044f\u0435\u0442 \u0440\u0435\u0437\u0443\u043b\u044c\u0442\u0430\u0442 \u0432 \u043a\u0430\u043d\u0430\u043b, \u0434\u0440\u0443\u0433\u0430\u044f \u0433\u043e\u0440\u0443\u0442\u0438\u043d\u0430, \u0432 \u043d\u0430\u0448\u0435\u043c \u0441\u043b\u0443\u0447\u0430\u0435 main, \u0447\u0438\u0442\u0430\u0435\u0442 \u0440\u0435\u0437\u0443\u043b\u044c\u0442\u0430\u0442 \u0438\u0437 \u043a\u0430\u043d\u0430\u043b\u0430.<\/p>\n<pre><code class=\"go\">func worker(jobs &lt;-chan int, results chan&lt;- int, wg *sync.WaitGroup) {       defer wg.Done()          for j := range jobs {          time.Sleep(1 * time.Second)          fmt.Println(\"job\", j)          results &lt;- j * j       }      }      func main() {       jobs := make(chan int)       results := make(chan int)          var wg sync.WaitGroup          for i := 0; i &lt; 3; i++ {          wg.Add(1)          go worker(jobs, results, &amp;wg)       }          go func() {          for i := 0; i &lt; 10; i++ {             jobs &lt;- i          }          close(jobs)       }()          go func() {          wg.Wait()          close(results)       }()          for result := range results {          fmt.Println(result)       }   }<\/code><\/pre>\n<\/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\/895922\/\"> https:\/\/habr.com\/ru\/articles\/895922\/<\/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-453850","post","type-post","status-publish","format-standard","hentry"],"_links":{"self":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/posts\/453850","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=453850"}],"version-history":[{"count":0,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/posts\/453850\/revisions"}],"wp:attachment":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=453850"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=453850"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=453850"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}