When an error occurs in the cooperative process, how can the upstream be obtained?

1. The scenario goes something like this: you need to get the number of new users in a certain period of time, which I plan to do with Ctrip. The preliminary code is similar to the following:

fansNum := make(chan string)
go fans.GetFansIncrementNum(allFansIds, todayStart, todayEnd, fansNum)

the statistics are returned to the upstream through fansNum, but what if there is an error in obtaining the number of users by fans.GetFansIncrementNum? I can think of a process: fansNum is defined as a make (chan map [string] interface {}). Add an error code to this map, and the upstream uses the error code to decide what to do with it. Excuse me, if there is an error in Ctrip, how does it return to the upstream?

Nov.12,2021

  1. pass GetFansIncrementNum a chan error , then select fansNum and errChan
  2. use context package,
  3. if you don't need to get the specific value of error, you can turn off fansNum directly in the GetFansIncrementNum function

personal recommendation 2


be smart:

anyway, your fansNum is intended to be used to pass the results, so why not define a result

  

after an error occurs in the co-program, call panic, and then use defer recover to catch the error, and then the error is handled. You can notify

up through the co-program.
Menu