I can't understand howcome only the loop part of total() function being run 3 times not the whole function. res := 0 runs only once while range part runs 3 times. My background is in Python and Java and the way total function is run not making sense at all.
评论:
kl0nos:
hayzeus:Because range on channel blocks if there is nothing to receive. You need to learn about channels in Go.
Read up on channels and goprocs. You have started a background function (total()), which is going to read from channel
in
, adding tores
untilin
is closed. Then the for loop end and the result is sent on theout
channel back tomain
total
is NOT run three times.
