How to debug the func of makeSlice Or the map creating or growing process step by step

42 views
Skip to first unread message

Chai Baoyang

unread,
Aug 7, 2020, 1:13:53 AM8/7/20
to golang-nuts
Hi All,
       I want to know how the slice or map grow.
       Do we have an method to debug the func of  makeSlice Or the map creating or growing process step by step?

Brian Candler

unread,
Aug 7, 2020, 7:14:42 AM8/7/20
to golang-nuts
To grow a slice, as a programmer you would do:

    myslice = append(myslice, value)

    myslice = append(myslice, anotherslice...)

Or you can do this my hand, by allocating a new slice of the required size, and copying the elements you want to keep.

There is a great overview of how this works internally at https://blog.golang.org/slices

To grow a map you just set a key to a value.  There is outline information here: https://blog.golang.org/maps

I'm sure I read an article which went into detail about the hashing algorithm, and under what conditions the hash grows, but I can't find it now.
The details are of course in the source code:

Reply all
Reply to author
Forward
0 new messages