packagemainimport("fmt""math/rand""time")typeMoostruct{CowintSoundstringTubechanbool}//Acowwillmoountilitisbeingfedfunccow(numint,mootubechanMoo){tube:=make(chanbool)for{select{casemootube<-Moo{num,"moo",tube}:fmt.Println("Cow number",num,"mooed through the mootube")<-tubefmt.Println("Cow number",num,"is being fed and stops mooing")mootube<-Moo{num,"mooh",nil}fmt.Println("Cow number",num,"moos one last time out of happyness")returndefault:fmt.Println("Cow number",num,"mooed through the mootube and was ignored")time.Sleep(time.Duration(rand.Int31n(1000))*time.Millisecond)}}}//Thefarmerwantstoputfoodinallthemootubestostopthemooingfuncfarmer(numcowsint,mootubechanMoo,farmertubechanstring){fmt.Println("Farmer starts listening to the mootube")forhungryCows:=numcows;hungryCows>0;{moo:=<-mootubeifmoo.Sound=="mooh"{fmt.Println("Farmer heard a moo of relief from cow number",moo.Cow)hungryCows--}else{fmt.Println("Farmer heard a",moo.Sound,"from cow number",moo.Cow)time.Sleep(2e9)fmt.Println("Farmer starts feeding cow number",moo.Cow)moo.Tube<-true}}fmt.Println("Farmer doesn't hear a single moo anymore. All done!")farmertube<-"yey!"}//ThefarmstartsoutwithmooingcowsthatwantstobefedfuncrunFarm(numcowsint){farmertube:=make(chanstring)mootube:=make(chanMoo)forcownum:=0;cownum<numcows;cownum++{gocow(cownum,mootube)}gofarmer(numcows,mootube,farmertube)farmerSaid:=<-farmertubeiffarmerSaid=="yey!"{fmt.Println("All cows are happy.")}}funcmain(){runFarm(4)fmt.Println("done")}
%%{init: {'theme': 'forest', 'fill': 'white', 'securitylevel': 'loose' } }%%
sequenceDiagram
participant Alice
participant Bob
Alice->>John: Hello John, how are you?
loop Healthcheck
John->>John: Fight against hypochondria
end
Note right of John: Rational thoughts prevail!
John-->>Alice: Great!
John->>Bob: How about you?
Bob-->>John: Jolly good!