(Cherolyn Lexvold) #1

What part of this code is/are the Goroutine(s)

Also Studying WaitGroup.

I copied and pasted this code from GoDoc in the Go Payground.

What did I do wrong?

func (*WaitGroup) Add

func (wg *WaitGroup) Add(delta int)

Add adds delta,

Please explain this concept of adding delta.

(Norbert Melzer) #2

A goroutine is some concurrent worker, created by the go keyword. It will create a “worker” which calls the function given.

The same as in the other thread, you are pasting the code somewhere… It needs to be in mains body or in a function that gets called by main to see any effect.

It just adds whatever you pass in as delta to the internal counter. You can not access that counter directly, but only add arbitrary values to it (wg.Add(delta)), decrement the counter by 1 (wg.Done()) or wait for it to reach 0 (wg.Wait()).

So assuming you have a “fresh” waitgroup, then do wg.Add(6) and start go routines, and then do wg.Wait(), then at least 6 of the go routines need to call wg.Done() on that waitgroup to make the “parent” continue.

(Cherolyn Lexvold) #3

Will you demonstrate?

What’s delta?

From Quora…
1 Answer

Brian MacKay

Brian MacKay, works at Inntec

Updated Mar 16, 2018

Delta shows up in mathematics and various sciences, and it always refers to what changed .

When referring to a database, delta is an academic way of describing how a command changed the data. It might refer to which rows were deleted, or old values versus new values of individual rows.

It isn’t something most developers have to think about on a daily basis, however we benefit from it any time we use, for instance, database transactions.

Transactions allow us to make a series of changes to a database, but cancel them all as a group if something goes wrong. This prevents our data from ending up in a broken or confusing state.

Transactions are easy for us to use, but to make that feature work inside the database, some very clever people spent their a good bit of their careers obsessing about delta.

There is so much here that is foreign to me that I think possibly I’ll need to experience it before I understand it. I printed this and bookmarked it so I can come back to it in the future.

This whole thing is Greek to me.

(Norbert Melzer) #4

What shall I demonstrate? That code needs to be part of a function to not syntax error? You already produced enough examples of those…

That functions need to be called to have an effect? Thats trivial to understand, no example necessary… But since you wanted it:

As the code is, you won’t see any output, but once you uncomment line 8, such that it says foo() rather then // foo(), you will see an output. Thats the full story…

The hole paragraph was to explain it… At the end it is just an arbitrary name, choosen by the developers of the go standard library to denote a parameter that is used as a difference to its former value. This conforms to the usage of “delta” in science. But, it could have been diff, difference, n or x or even fnoobrickl, but especially the latter had no explaining context.

You do not need to understand why a variable is named how it is named, just use it the way its described.

Sorry, but I can’t explain it more simple.

This is how it works. The WaitGroup maintains internal inaccessible state, which you can add to, decrement in steps of 1 and wait for becoming 0.

This is as simple as one can get. I do not think, that going through the implementation would help you.

(Cherolyn Lexvold) #5

Sorry if I’m irritating people. How do I mark this as solved? I can study on my own, if you wish.