var name_age map[string]int
name := []string{"david", "avid", "stitches", "killeta", "sskrill"}
age := []int{20, 30, 18, 40, 34}
How do you insert slices name and age into name_age map?
I have tried;
for _, i := range names {
for _, x := range x {
name_age = map[string]int{i: x}
}
}
And also tried
for i := 0; i < len(name); i++ {
for x := 0; x < len(age); x++ {
mn = map[string]int{name[i]: age[x]}
}
}
the output in both loops is map[sskrill:34]
When I use
var name_age_x = make(map[string]int)
for i := 0; i < len(name); i++ {
for x := 0; x < len(age); x++ {
name_age_x[name[i]] = age[x]
}
}
The output is map[avid:34 david:34 killeta:34 sskrill:34 stitches:34] ////Age 34 is repeated in all names… /// Even when I use the range loop the result is the same
How do I get this working properly???
NB::: The order of Items should not be altered/sorted so that the age matches with the names.
We allocate u / n (integer division) items plus the u % n (integer division) remainder items to unallocated chunks. We allocate the u % n remainder items to the first u % n unallocated chunks.
The first u % n (may be zero) unallocated chunks will contain u / n + 1 items and the other chunks will contain u / n items for a total of u items in n chunks.
For u unallocated items (len(items)) and n unallocated chunks, m items, rounded up, are allocated to a chunk:
`m := (u + (n - 1)) / n`
then, for the next iteration,
`u = u - m` // items = items[m:]
`n = n - 1` // n--
We know that all the items will be allocated since, for the last chunk iteration (n == 1),
Thanks a bunch… I wish you had added comments so that i can understand what is really going on. I have been fearing things to do with arrays and slices.
I guess my head just cant wrap around how they work but with time i’ll finally understand.
Thanks for the answer and I would really appreciate if you led m into th way of undestanding how arrays work and do they work the same in all languages.
func chunker(items []string, n int) [][]string {
chunks := make([][]string, n)
for i := 0; i < len(chunks); i++ {
m := (len(items) + (n - 1)) / n
chunks[i] = items[:m]
items = items[m:]
n–
}
return chunks
}
This looks more intresting since it doesn’t have an if condition inside it
if you learn to use a step debugger and it would have been painfully apparent in about 3 seconds why your code was wrong and what it was doing and how to fix it.
Assigns the value of the subslice items[:m:m] to the variable chunks[i].
items[:m:m]
is a full slice expression.
m := (len(items) + (n - 1)) / n
m is the number of unallocated items to be allocated to chunk[i].
items = items[m:]
items[m:] is a simple slice expression. The items variable represents the unallocated items. Once we have allocated m items, update items. This updates the value of len(items).
You didn’t copy my code correctly. What are you trying to accomplish?
If you insert a debug print statement in my code you will get sensible results.
Thanks… This now makes me understand it completely… was wondering what’s the purpose of declaring items = items[m:]. Now I get that it is used to update the value of len(items)
Thanks. Had no idea about a step debugger. I am self learning in programming so dont have a guide on how things work around.
Started with java but when I got into db CRUDs gave up, researched online other programming languages tried out a few and fell in love with Golang because of its simplicity and its ease in db connection and set up and its somehow oop nature. Though receivers and & are new and abit complicated to get used to.
knowing your tools is the biggest part of the job. step debuggers are the best way of debugging, they show the the state of everything and you can see in “slow motion” one step at a time as things change.
Thanks guys for all the solutions and assistace, I have really learnt a lot from u.
Now i want to insert the chunks in a html page but it is not working
I already posted the issue here.