https://play.golang.org/p/wi9QlhBGTfr


(Justin) #1

Trying to send some data using channel. First is a struct, and the other
is for telling me my work is done using signals. Received the two
channels and ran a select for them. In main, i ran two goroutines, but
unfortunately, dont get my result

//use this link to get to the code
https://play.golang.org/p/wi9QlhBGTfr

Should get something like this
//this is the expected result
[widget_0 22:26:15.454569]
[widget_1 22:26:15.454594]
[widget_2 22:26:15.454792]
[widget_3 22:26:15.454792]

(Norbert Melzer) #2

Your main go routine exits before anything else can happen, you need to find a way to make it wait for the other go routines beeing finished.


(Justin) #3

Thanks. For some reason i forgot about waitgroups


(Justin) #4

https://play.golang.org/p/_oMyr7sLTB7
The issue with waiting was resolved. I implemented the os.Signal, so that when i give it a signal, (ctrl + c) it should end my work. For some reason it ignores that


(Norbert Melzer) #5

Well, you first wait for a signal on d, then you wait until the waitgroup has been satisfied.


(Justin) #6

Im not sure i understood. Mind telling me what you mean?


(Norbert Melzer) #7
	fmt.Println("awaiting signal")
	<-d
	wg.Wait()

These are sequential operations,

  1. print a message
  2. wait for a message on d
  3. wait for the waitgroup

So your program will never be able to finish without you beeing active and sending it one of the signals specified in your code, eg via CTRL-c in the terminal that runs the program, and the waitgroup gets satisfied by an amount of calls to Done() that is equivalent to the count summed up using calls to Add().


(Justin) #8

https://play.golang.org/p/e5RyOiaN3nM
Thanks. Resolved the issue. But i noticed i only get even numbers, and ive never been more confused before in my life why its giving me that…Ran 10 goroutines, and added a wg in the cycle everytime it runs…still i dont get all the goroutines running.
https://play.golang.org/p/e5RyOiaN3nM


(Norbert Melzer) #9
	for i := 0; i < 10; i++ {
		// …
		i++
	}

i gets incremented twice per cycle. Once in the loops body and once in the loops “head”. In this kind of loop you shouldn’t mutate the loop variable inside the body unless you know what you are doing.


(Justin) #10

Thanks. Sorry, may i ask why this error -
panic: non-positive interval for NewTicker.
Same code when i fix the i++ issue


(Norbert Melzer) #11

Because you generate a random integer that is greater than or equal to 0 but less than 5, you use this to create a timer, those needs to be created with a non negative and non-zero duration.


(Justin) #13

https://play.golang.org/p/y1y78WmBqFn
i made my number to not be 0 and i still got the result.
tried making it always > 0. same thing


(Norbert Melzer) #14
	n := 5
	r := rand.Intn(n)

This still allows for r beeing 0.

r := rand.Intn(n-1) + 1

This will give you an r that adheres to 0 < r && r < n.


(Justin) #15

I need to read more books. Thanks for you help. Can you recommend any material i can read? so i can reduce the amount of dumb questions i ask


(Norbert Melzer) #16

The only “dumb questions” are those that don’t get asked, as my Prof used to say…

But sadly I can’t give any reading recommondations, as I usually learn by using a language and reading API documentation.

Have not used a real book (for programming) since starting with Commodore Basic v3.5 30-ish years ago…

I do learn much better by doing and perhaps talking, than just reading. Sometimes I follow some tutorials from the web, but thats rare.

Though I can for sure recommend exercism.io for learning katas. In mentored mode even real humans will give you feedback, though it might take a while before that happens and you can move on to the next exercises.


(Justin) #19

Hey, was wondering if you could help me out…
https://play.golang.org/p/0U5_YrAYW7o
The comments explain a lot in the code.
Im trying to make a function that returns the type of animal, and pass that function as a parameter. There since to be a problem with type conversion.
The three send functons are sending me the info i need.
https://play.golang.org/p/0U5_YrAYW7o
if i didnt explain the question properly, please inform me