[go] How I can organize this code

I have 2 arrays

arr1 array[100] elements

arr2 array[1000000] elements

How can I loop through arr2 and assign every 100 elements of the 1st array and make it multithreaded?

pls help)

Try to solve it in a single go routine first. Once you have that code we can discuss that and help you to make it concurrent.

Aside of that, I’m not sure if it is worth the effort, spreading this work over multiple go-routins might be less efficient than doing it in a single, due to the small overhead go routines introduce and the fact that the data has to be moved from memory and CPU and back, and this is through a shared data bus.

1 Like

there is that I want to do

https://play.golang.org/p/0r55y2HD3V-

I wrote simple code, but the values in the array did less, in the arr2 array there will be 1,000,000,000 values, and how do I implement a similar code that I brought multi-threaded, for example, I indicated 100, there would be 100 threads, or something different?

who will help me in my situation on the example code

Just printing the contents looks so much easier if you use the modulo operation:

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

But in your first post, you said you want to initialise the bigger slice from the smaller one, also there only were two slices. Now you have three.

Could you please elaborate?

1 Like

I love you, thank you, but for example I wrote that there will be only 2 arrays, in fact, I didn’t understand all the mechanics myself, but I think your example is just what I need (modulo operation).

The idea is that in each line I will write such data into 1 large file, in each line there will be data taken from the last cycle:

arr2 [i], arr1 [i% len (arr1)], arr0 [i% len (arr0)] ..

So, because of the presence of such a large number of cycles, I just don’t know how to apply multithreading (and moreover, write all this to a file (

But from your post, I learned how easy it is to work (modulo operation)

or is it better to do such an operation with 1 thread?

Cuncurrently writing into a single file is not a good idea.

  1. It will write your items out of order
  2. It might even interleave the written data

So either of these might happen (simplified examples)

Line 1
Line 2
Line 2
Line 1
LinLine 2
e 1
LiLine 1
ne 2
LiLinene 1
 2

Hi. This is just a tight loop without any access to disk, network or other things that make the go routine wait (and then allow other go routines time to run). So the maximum number of go routines which could make work at the same time is the number of (logical) cores you got. You can get this number from

runtime.NumCPU()

So on a normal CPU with 2 or 4 cores could you maybe run this many go routines in parallell. However it may take longer if the array must be shuffled back and forth between different levels of cache to ensure the cores sees the same contents of the memory.

1 Like

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.