Observer pattern in Go language - Stack Overflow

文章推薦指數: 80 %
投票人數:10人

The Go way to implement the Observer design pattern - Stack ... Home Public Questions Tags Users Collectives ExploreCollectives FindaJob Jobs Companies Teams StackOverflowforTeams –Collaborateandshareknowledgewithaprivategroup. CreateafreeTeam WhatisTeams? Teams CreatefreeTeam CollectivesonStackOverflow Findcentralized,trustedcontentandcollaboratearoundthetechnologiesyouusemost. Learnmore Teams Q&Aforwork Connectandshareknowledgewithinasinglelocationthatisstructuredandeasytosearch. Learnmore ObserverpatterninGolanguage AskQuestion Asked 11years,3monthsago Active 1monthago Viewed 8ktimes 16 8 Thisproblemisprettycommon:anobjectshouldnotifyallitssubscriberswhensomeeventoccurs.InC++wemayuseboost::signalsorsomethingelse.ButhowtodothisinGolanguage?Itwouldbenicetoseesomeworkingcodeexamplewhereacoupleofobjectsaresubscribedtoapublisherandprocessnotifications. Thanks goobserver-pattern Share Follow editedDec25'18at23:16 FedirRYKHTIK 9,38666goldbadges5353silverbadges6565bronzebadges askedSep17'10at8:29 StasStas 10.9k66goldbadges3737silverbadges5555bronzebadges Addacomment  |  2Answers 2 Active Oldest Votes 20 ThisisactuallyprettysimpleinGo.Usechannels.Thisisthekindofthingthey'remadefor. typePublishstruct{ listeners[]chan*Msg } typeSubscriberstruct{ Channelchan*Msg } func(p*Publisher)Sub(cchan*Msg){ p.appendListener(c) } func(p*Publisher)Pub(m*Msg){ for_,c:=rangep.listeners{ c



請為這篇文章評分?