This website uses strictly necessary cookies & statistics cookies to provide functionality and to analyze traffic.

Go Maps Explained: How Key-Value Pairs Are Actually Stored

If you’re new to Go, it can be a bit confusing to figure out how to use maps in Go. And even when you’re more experienced, understanding how maps really work can be pretty tough.

Take this example: Have you ever set a ‘hint’ for a map and wondered why it’s called a ‘hint’ and not something simple like length, like we do with slices?

Or maybe you’ve noticed that when you use a for-range loop on a map, the order doesn’t match the insertion order, and it even changes if you loop over the same map at different times. But weirdly enough, if you loop over it at the exact same time, the order usually stays the same.

This is a long story, so fasten your seat belt and dive in.

Before we move on, just a heads up—the info here is based on Go 1.23. If things have changed and this isn’t up to date, feel free to ping me on X(@func25) .

Map in Go: Quick Start

So, let’s talk about maps in Go, it’s a built-in type that acts as a key-value storage. Unlike arrays where you’re stuck with keys as increasing indices like 0, 1, 2, and so on, with maps, the key can be any comparable type.

That gives you a lot more flexibility.

In that example, we created an empty map using make(), where the keys are strings and the values are ints.

Now, instead of manually assigning each key, you can save yourself some time by using a map literal. This lets you set up your key-value pairs all at once, right when you create the map:

All you do is list out the keys and their values inside curly braces when you first create the map, simple as that.

And if you realize later that you don’t need a certain key-value pair anymore, Go’s got you covered. There’s a handy delete function that, well, deletes the key you don’t want: delete(m, "a") .

The zero value of a map is nil , and a nil map is kind of like an empty map in some ways. You can try to look up keys in it, and Go won’t freak out and crash your program.

If you search for a key that isn’t there, Go just quietly gives you the “zero value” for that map’s value type:

But here’s the thing: you can’t add new key-value pairs to a nil map.

In fact, Go handles maps in a way that’s pretty similar to how it deals with slices. Both maps and slices start off as nil , and Go doesn’t panic if you do something “harmless” with them while they’re nil. For example, you can loop over a nil slice without any “drama”.

So, what happens if you try to loop over a nil map?

Nothing happens, no errors, no surprises. It just quietly does nothing.

Go’s approach is to treat the default value of any type as something useful, not something that causes your program to blow up. The only time Go throws a fit is when you do something that’s truly illegal, like trying to add a new key-value pair to a nil map or accessing an out-of-bounds index in a slice.

There are a couple more things you should know about maps in Go:

  • A for-range loop over a map won’t return the keys in any specific order.
  • Maps aren’t thread-safe, the Go runtime will cause a fatal error if you try to read (or iterate with a for-range) and write to the same map simultaneously.
  • You can check if a key is in a map by doing a simple ok check: _, ok := m[key] .
  • The key type for a map must be comparable.

Let’s dive into that last point about map keys. I mentioned earlier that “the key could be any comparable type,” but there’s a bit more to it than just that.

“So, what exactly is a comparable type? And what isn’t?”

It’s pretty simple: if you can use == to compare two values of the same type, then that type is considered comparable.

But as you can see, the code above doesn’t even compile. The compiler complains: “invalid operation: s == s (map can only be compared to nil).”

This same rule applies to other non-comparable types like slices, functions, or structs that contain slices or maps, etc. So, if you’re trying to use any of those types as keys in a map, you’re out of luck.

But here’s a little secret, interfaces can be both comparable and non-comparable.

What does that mean? You can absolutely define a map with an empty interface as the key without any compile errors. But watch out, there’s a good chance you’ll run into a runtime error.

Everything looks fine until you try to assign an uncomparable type as a map key.

That’s when you’ll hit a runtime error, which is trickier to deal with than a compile-time error. Because of this, it’s usually a good idea to avoid using interface{} as a map key unless you have a solid reason and constraints that prevent misuse.

But that error message: “hash of unhashable type []int” might seem a bit cryptic. What’s this about a hash? Well, that’s our cue to dig into how Go handles things under the hood.

Map Anatomy

When explaining the internals of something like a map, it’s easy to get bogged down in the nitty-gritty details of the Go source code. But we’re going to keep it light and simple so even those new to Go can follow along.

What you see as a single map in your Go code is actually an abstraction that hides the complex details of how the data is organized. In reality, a Go map is composed of many smaller units called “buckets.”

Look at Go source code above, a map contains a pointer that points to the bucket array.

This is why when you assign a map to a variable or pass it to a function, both the variable and the function’s argument are sharing the same map pointer.

But don’t get it twisted, maps are pointers to the hmap under the hood, but they aren’t reference types, nor are they passed by reference like a ref argument in C#, if you change the whole map m2 , it won’t reflect on the original map m1 in the caller.

In Go, everything is passed by value. What’s really happening is a bit different: when you pass the map m1 to the changeMap function, Go makes a copy of the *hmap structure. So, m1 in the main() and m2 in the changeMap() function are technically different pointers point to the same hmap .

For more on this topic, there’s a great post by Dave Cheney titled There is no pass-by-reference in Go .

Each of these buckets can only hold up to 8 key-value pairs, as you can see in the image below.

The map above has 2 buckets, and len(map) is 6.

So, when you add a key-value pair to a map, Go doesn’t just drop it in there randomly or sequentially. Instead, it places the pair into one of these buckets based on the key’s hash value, which is determined by hash(key, seed) .

Let’s see the simplest assignment scenario in the image below, when we have an empty map, and assign a key-value pair "hello": 1 to it.

It starts by hashing “hello” to a number, then it takes that number and mods it by the number of buckets.

Since we only have one bucket here, any number mod 1 is 0, so it’s going straight into bucket 0 and the same process happens when you add another key-value pair. It’ll try to place it in bucket 0, and if the first slot’s taken or has a different key, it’ll move to the next slot in that bucket.

Take a look at the hash(key, seed) , when you use a for-range loop over two maps with the same keys, you might notice that the keys come out in a different order:

How’s that possible? Isn’t the key “a” in map a and the key “a” in map b hashed the same way?

But here’s the deal, while the hash function used for maps in Go is consistent across all maps with the same key type , the seed used by that hash function is different for each map instance. So, when you create a new map, Go generates a random seed just for that map.

In the example above, both a and b use the same hash function because their keys are string types, but each map has its own unique seed.

“Wait, a bucket has only 8 slots? What happens if the bucket gets full? Does it grow like a slice?”

Well, sort of. When the buckets start getting full, or even almost full, depending on the algorithm’s definition of “full”, the map will trigger a growth, which might double the number of main buckets.

But here’s where it gets a bit more interesting.

When I say “main buckets,” I’m setting up for another concept: “overflow buckets.” These come into play when you’ve got a situation with high collisions. Imagine you have 4 buckets, but one of them is completely filled with 8 key-value pairs due to high collisions, while the other 3 buckets are sitting empty.

Do you really need to grow the map to 8 buckets just because you need to add one more entry that, unfortunately, also lands in that first full bucket?

Absolutely not, right? It’d be pretty wasteful to double the number of buckets in that case.

Instead, Go handles this more efficiently by creating “overflow buckets” that are linked to the first bucket. The new key-value pair gets stored in this overflow bucket rather than forcing a full grow.

A map in Go grows when one of two conditions is met: either there are too many overflow buckets, or the map is overloaded, meaning the load factor is too high.

Because of these 2 conditions, there are also two kinds of growth:

  • One that doubles the size of the buckets (when overloaded)
  • One that keeps the same size but redistributes entries (when there are too many overflow buckets).

If there are too many overflow buckets, it’s better to redistribute the entries rather than just adding more memory.

Currently, Go’s load factor is set at 6.5, this means the map is designed to maintain an average of 6.5 items per bucket, which is around 80% capacity. When the load factor goes beyond this threshold, the map is considered overloaded. In this case, it will grow by allocating a new array of buckets that’s twice the size of the current one, and then rehashing the elements into these new buckets.

The reason we need to grow the map even when a bucket is just almost full comes down to performance. We usually think that accessing and assigning values in a map is O(1), right? But it’s not always that simple.

The more slots in a bucket that are occupied, the slower things get.

When you want to add another key-value pair, it’s not just about checking if a bucket has space, it’s about comparing the key with each existing key in that bucket to decide if you’re adding a new entry or updating an existing one.

And it gets even worse when you have overflow buckets, because then you have to check each slot in those overflow buckets too. This slowdown affects access and delete operations as well.

But Go team got your back, they optimized this comparison for us.

Remember the hash we got when hashing the key “Hello”? Go doesn’t just toss that away. It actually caches the tophash of “Hello” in the bucket as a uint8 , and uses that for a quick comparison with the tophash of any new key. This makes the initial check super fast.

After comparing the tophash , if they match, it means the keys “might” be the same. Then, Go moves on to the slower process of checking whether the keys are actually identical.

“Why does creating a new map with make(map, hint) not provide an exact size but just a hint?”

By now, you’re probably in a good spot to answer this one. The hint parameter in make(map, hint) tells Go the initial number of elements you expect the map to hold.

This hint helps minimize the number of times the map needs to grow as you add elements.

Since each growth operation involves allocating a new array of buckets and copying existing elements over, it’s not the most efficient process. Starting with a larger initial capacity can help avoid some of these costly growth operations.

Let me give you a real-world look at how the bucket size grows as you add more elements:

Hint RangeBucket CountCapacity
0 - 818
9 - 13216
14 - 26432
27 - 52864
53 - 10416128
105 - 20832256
209 - 41664512
417 - 8321281024
833 - 16642562048
“Why does a hint of 14 result in 4 buckets? We only need 2 buckets to cover 14 entries.”

This is where the load factor comes into play, remember that load factor threshold of 6.5? It directly influences when the map needs to grow.

  • At hint 13, we have 2 buckets and that gives a load factor of 13/2 = 6.5, which hits the threshold but doesn’t exceed it. So, when you bump up to hint 14, the load factor would exceed 6.5, triggering the need to grow.
  • The same goes for hint 26. With 4 buckets, the load factor is 26/4 = 6.5, again hitting that threshold. When you move beyond 26, the map needs to grow to accommodate more elements efficiently.

Basically, from the second range, you can see that the hint range doubles compared to the previous one, as do the bucket count and capacity.

Evacuation When Growing

As we talked about earlier, evacuation doesn’t always mean doubling the size of the buckets. If there are too many overflow buckets, evacuation will still happen, but the new bucket array will be the same size as the old one.

The more interesting scenario is when the bucket size doubles, so let’s focus on that.

Map growth answers two common questions: “Why can’t you get the address of a map’s element?” and “Why isn’t the for-range order guaranteed at different times?”

When a map grows, it allocates a new bucket array that’s double the size of the old one. All the entry positions in the old buckets become invalid, and they need to be moved to the new buckets with new memory addresses.

And the thing is, if your map has, say, 1000 key-value pairs, moving all those keys at once would be a pretty expensive operation, potentially blocking your goroutine for a noticeable chunk of time. To avoid that, Go uses “incremental growth,” where only a portion of the elements are rehashed at a time.

This way, the process is spread out, and your program keeps running smoothly without sudden lagging.

The process gets a bit more complex because we need to maintain the map’s integrity while reading, writing, deleting, or iterating, all while managing both the old and new buckets.

“When does incremental growth happen?”

There are only two scenarios where incremental growth kicks in: when you assign a key-value pair to a map or when you delete a key from a map. Either action will trigger the evacuation process and at least one bucket is moved to the new bucket array.

When you assign something like m["Hello"] = 2 , if the map is in the middle of growing, the first thing it does is evacuate the old bucket containing the Hello key.

Each element in that old bucket gets moved to one of two new buckets and this same process applies even if the map has more than 2 buckets.

For example, if you’re growing from 4 buckets to 8, the elements in the old bucket 1 will either move to the new bucket 1 or the new bucket 5 . How do we know that? It’s just a bit of math involving a bitwise operation.

If a hash % 4 == 1 , it means the hash % 8 == 1 or hash % 8 == 5 . Because for an old bucket where H % 4 == 1 , the last two bits of H are 01 . When we consider the last three bits for the new bucket array:

  • If the third bit (from the right) is 0, the last three bits are 001, which means H % 8 == 1 .
  • If the third bit (from the right) is 1, the last three bits are 010, which means H % 8 == 5 .

If the old bucket has overflow buckets, the map also moves the elements from these overflow buckets to the new buckets. After all elements have been moved, the map marks the old bucket as “evacuated” through the tophash field.

That’s all for today discussion, Go map is indeed more complicated than that, with many tiny details that we don’t mention here, for instance, the tophash is not only used for comparison but also for evacuation.

Stay Connected

The author’s writing style emphasizes clarity and simplicity. Instead of using complex, textbook-style definitions, we explain concepts in a way that’s easy to understand, even if it’s not always perfectly aligned with academic precision.

If you spot anything that’s outdated or if you have questions, don’t hesitate to reach out. You can drop me a DM on X(@func25) .

Some other posts you might be interested in:

  • Golang Sync Mutex: Normal and Starvation Mode
  • How Go Arrays Work and Get Tricky with For-Range
  • Golang Defer: From Basic To Traps
  • Vendoring, or go mod vendor: What Is It?

If you want to monitor your services, track metrics, and see how everything performs, you might want to check out VictoriaMetrics . It’s a fast, open-source , and cost-saving way to keep an eye on your infrastructure.

And we’re Gophers, enthusiasts who love researching, experimenting, and sharing knowledge about Go and its ecosystem.

Watch Your Monitoring SkyRocket With VictoriaMetrics!

  • VictoriaMetrics
  • VictoriaMetrics Enterprise
  • Monitoring of Monitoring
  • VictoriaMetrics Cloud
  • VictoriaMetrics Anomaly Detection
  • Plans & Features
  • VictoriaLogs
  • Status Page
  • GitHub Repo
  • Case Studies
  • Our Customers
  • News & Articles
  • 24hr Support
  • Privacy Policy
  • Disclosure Policy
  • Cookie Use Policy
  • Enterprise Solution Agreement

Many thanks for signing up for our newsletter. You have been added to our distribution list and you will receive our newsletter accordingly. Note that you can unsubscribe at any time from within the newsletter.

Many thanks for submitting our Contact Us form. One of our team members will be in touch shortly to follow up on it with you.

Maps explained: create, add, get, delete

Go maps are implemented by hash tables and have efficient add, get and delete operations.

go map assignment

Create a new map

Add, update, get and delete keys/values, for-each range loop, performance and implementation.

  • A map (or dictionary) is an unordered collection of key-value pairs, where each key is  unique .
  • You create a new map with a make statement or a map literal .
  • The default zero value of a map is nil . A nil map is equivalent to an empty map except that elements can’t be added .
  • The len function returns the size of a map, which is the number of key-value pairs.
Warning: If you try to add an element to an uninitialized map you get the mysterious run-time error Assignment to entry in nil map .
  • When you index a map you get two return values; the second one (which is optional) is a boolean that indicates if the key exists.
  • If the key doesn’t exist, the first value will be the default zero value .
  • Iteration order is not specified and may vary from iteration to iteration.
  • If an entry that has not yet been reached is removed during iteration, the corresponding iteration value will not be produced.
  • If an entry is created during iteration, that entry may or may not be produced during the iteration.
Starting with Go 1.12 , the fmt package prints maps in key-sorted order to ease testing.
  • Maps are backed by hash tables .
  • Add, get and delete operations run in constant expected time. The time complexity for the add operation is amortized .
  • The comparison operators == and != must be defined for the key type.

Go step by step

go map assignment

Core Go concepts: interfaces , structs , slices , maps , for loops , switch statements , packages .

Golang Maps by Example

Golang Maps by Example

A map is an unordered collection of key-value pairs. It maps keys to values. The keys are unique within a map while the values may not be.

The map data structure is used for fast lookups, retrieval, and deletion of data based on keys. It is one of the most used data structures in computer science.

Go provides a built-in map type. In this article, we’ll learn how to use Golang’s built-in map type.

Declaring a map

A map is declared using the following syntax -

For example, Here is how you can declare a map of string keys to int values -

The zero value of a map is nil . A nil map has no keys. Moreover, any attempt to add keys to a nil map will result in a runtime error.

Let’s see an example -

If you uncomment the statement m["one hundred"] = 100 , the program will generate the following error -

It is, therefore, necessary to initialize a map before adding items to it.

Initializing a map

1. initializing a map using the built-in make() function.

You can initialize a map using the built-in make() function. You just need to pass the type of the map to the make() function as in the example below. The function will return an initialized and ready to use map -

Let’s see a complete example -

2. Initializing a map using a map literal

A map literal is a very convenient way to initialize a map with some data. You just need to pass the key-value pairs separated by colon inside curly braces like this -

Note that the last trailing comma is necessary, otherwise, you’ll get a compiler error.

Let’s check out a complete example -

You can also create an empty map using a map literal by leaving the curly braces empty -

The above statement is functionally identical to using the make() function.

Adding items (key-value pairs) to a map

You can add new items to an initialized map using the following syntax -

The following example initializes a map using the make() function and adds some new items to it -

If you try to add a key that already exists in the map, then it will simply be overridden by the new value.

Retrieving the value associated with a given key in a map

You can retrieve the value assigned to a key in a map using the syntax m[key] . If the key exists in the map, you’ll get the assigned value. Otherwise, you’ll get the zero value of the map’s value type.

Let’s check out an example to understand this -

In the above example, since the key "Jack" doesn’t exist in the map, we get the zero value of the map’s value type. Since the map’s value type is string , we get " " .

Unlike other languages, we do not get a runtime error in Golang if the key doesn’t exist in the map.

But what if you want to check for the existence of a key? In the above example, the map would return " " even if the key "Jack" existed with the value " " . So how do we distinguish between cases where a key exists with the value equal to the zero value of the value type, and the absence of a key?

Well, let’s find out.

Checking if a key exists in a map

When you retrieve the value assigned to a given key using the syntax map[key] , it returns an additional boolean value as well which is true if the key exists in the map, and false if it doesn’t exist.

So you can check for the existence of a key in a map by using the following two-value assignment -

The boolean variable ok will be true if the key exists, and false otherwise.

Consider the following map for example. It maps employeeIds to names -

Accessing the key 1001 will return "Rajeev" and true , since the key 1001 exists in the map -

However, If you try to access a key that doesn’t exist, then the map will return an empty string "" (zero value of strings), and false -

If you just want to check for the existence of a key without retrieving the value associated with that key, then you can use an _ (underscore) in place of the first value -

Now let’s check out a complete example -

In the above example, I’ve used a short declaration in the if statement to initialize the name and ok values, and then test the boolean value ok . It makes the code more concise.

Deleting a key from a map

You can delete a key from a map using the built-in delete() function. The syntax looks like this -

The delete() function doesn’t return any value. Also, it doesn’t do anything if the key doesn’t exist in the map.

Here is a complete example -

Maps are reference types

Maps are reference types. When you assign a map to a new variable, they both refer to the same underlying data structure. Therefore changes done by one variable will be visible to the other -

The same concept applies when you pass a map to a function. Any changes done to the map inside the function is also visible to the caller.

Iterating over a map

You can iterate over a map using range form of the for loop. It gives you the key, value pair in every iteration -

Note that, A map is an unordered collection and therefore the iteration order of a map is not guaranteed to be the same every time you iterate over it.

So if you run the above program multiple times, you’ll get the results in different orders.

Congratulations folks! In this article, you learned how to declare and initialize maps, how to add keys to a map, how to retrieve the value associated with a given key in a map, how to check for the existence of a key in a map, how to delete a key from a map, and how to iterate over a map.

Thank you for reading. Stay tuned for more such articles.

Next Article: Playing with Pointers in Golang
Code Samples: github.com/callicoder/golang-tutorials

Go maps in action

Keith Randall - Inside the Map Implementation

Share on social media

Golang Maps Tutorial

Welcome to tutorial no. 13 in Golang tutorial series .

What is a map?

A map is a inbuilt data type in Go which is used to store key-value pairs. A practical use case for a map is for storing the currency codes and the corresponding currency names.

A map will be a perfect fit for the above use case use case. The currency code can be the key and the currency name can be the value. Maps are similar to dictionaries in other languages such as Python.

How to create a map?

A map can be created by passing the data type of key and value to the make function. The following is the syntax to create a new map.

The above line of code creates a map named currencyCode which has string keys and string values.

Run in Playground

The program above creates a map named currencyCode with string key and string value. The above program will print,

Since we have not added any elements to the map, it’s empty.

Adding items to a map

The syntax for adding new items to a map is the same as that of arrays . The program below adds some new currency codes and currency names to the currencyCode map.

Run in playground

We have added 4 currencyCodes namely USD , GBP , EUR , INR and their corresponding names.

The above program prints,

As you might have recognized from the above output, the order of the retrieval of values from a map is not guaranteed to be the same as the order in which the elements were added to the map.

It is also possible to initialize a map during the declaration itself.

The above program declares currencyCode map and adds 3 items to it during the declaration itself. Later one more element with key INR is added. The program prints

It’s not necessary that only string types should be keys. All comparable types such as boolean, integer, float, complex, string, … can also be keys. Even user-defined types such as structs can be keys. If you would like to know more about comparable types, please visit https://go.dev/ref/spec#Comparison_operators

nil map panics

The zero value of a map is nil . If you try to add elements to a nil map, a run-time panic will occur. Hence the map has to be initialized before adding elements.

In the above program, currencyCode is nil and we are trying to add a new key to a nil map. The program will panic with error

panic: assignment to entry in nil map

Retrieving value for a key from a map

Now that we have added some elements to the map, let’s learn how to retrieve them. map[key] is the syntax to retrieve elements of a map.

The above program is pretty straightforward. The currency name for the currency code USD is retrieved and printed. The program prints

What will happen if an element is not present? The map will return the zero value of the type of that element. In the case of currencyCode map, if we try to access an item which is not present, the zero value of string, “"(the empty string) is returned.

The output of the above program is

The above program returns empty string as the currency name for INR . There will be no runtime error when we try to retrieve the value for a key that is not present in the map.

Checking if a key exists

In the above section we learned that when a key is not present, the zero value of the type will be returned. This doesn’t help when we want to find out whether the key actually exists in the map.

For example, we want to know whether a currency code key is present in the currencyCode map. The following syntax is used to find out whether a particular key is present in a map.

ok in the above line of code will be true when the key is present and the value for the key is present in the variable value . If the key is not present, ok will be false and the zero value is returned for value .

In the above program, in line no. 14, ok will be false since INR key is not present. Hence the program will print,

Iterate over all elements in a map

The range form of the for loop is used to iterate over all elements of a map.

The above program outputs,

One important fact to note is the order of the retrieval of values from a map when using for range is not guaranteed to be the same for each execution of the program. It is also not the same as the order in which the elements were added to the map

Deleting items from a map

delete(map, key) is the syntax to delete key from a map . The delete function does not return any value.

The above program deletes the key EUR and it prints

Even if we try to delete a key that is not present in the map, there will be no runtime error.

Map of structs

So far we have only been storing the currency name in the map. Wouldn’t it be nice if we are able to store the symbol of the currency too? This can be achieved by using a map of structs . The currency can be represented as a struct containing the fields currency name and currency symbol. This struct value can be stored in the map with a currency code as key . Let’s write a program to understand how this can be done.

In the above program, currency struct contains fields name and symbol . We create three currencies curUSD , curGBP and curEUR .

In line no. 26, we initialize a map with string key and value of type currency with the three currencies we created.

The map is iterated in line no. 32 and the currency details are printed in the next line. This program will print,

Length of the map

Length of the map can be determined using the len function.

len(currencyCode) in the above program returns the length of the map. The above program prints,

Maps are reference types

Similar to slices , maps are reference types. When a map is assigned to a new variable, they both point to the same underlying data structure. Hence changes made in one will reflect in the other.

In line no. 14 of the above program, employeeSalary is assigned to modified . In the next line, the salary of mike is changed to 18000 in the modified map. Mike’s salary will now be 18000 in employeeSalary too. The program outputs,

Similar is the case when maps are passed as parameters to functions. When any change is made to the map inside the function, it will be visible to the caller also.

Maps equality

Maps can’t be compared using the == operator. The == can be only used to check if a map is nil .

The above program will fail to compile with error

One way to check whether two maps are equal is to compare each one’s individual elements one by one. The other way is using reflection . I would encourage you to write a program for this and make it work :).

This brings us to the end of this tutorial. Hope you enjoyed it. Please leave your feedback and comments. Please consider sharing this tutorial on twitter and LinkedIn . Have a good day.

Next tutorial - Strings

go map assignment

This package is not in the latest version of its module.

Documentation ¶

Package maps defines various functions useful with maps of any type.

  • func Clear[M ~map[K]V, K comparable, V any](m M)
  • func Clone[M ~map[K]V, K comparable, V any](m M) M
  • func Copy[M1 ~map[K]V, M2 ~map[K]V, K comparable, V any](dst M1, src M2)
  • func DeleteFunc[M ~map[K]V, K comparable, V any](m M, del func(K, V) bool)
  • func Equal[M1, M2 ~map[K]V, K, V comparable](m1 M1, m2 M2) bool
  • func EqualFunc[M1 ~map[K]V1, M2 ~map[K]V2, K comparable, V1, V2 any](m1 M1, m2 M2, eq func(V1, V2) bool) bool
  • func Keys[M ~map[K]V, K comparable, V any](m M) []K
  • func Values[M ~map[K]V, K comparable, V any](m M) []V

Constants ¶

This section is empty.

Variables ¶

Functions ¶, func clear ¶.

Clear removes all entries from m, leaving it empty.

func Clone ¶

Clone returns a copy of m. This is a shallow clone: the new keys and values are set using ordinary assignment.

func Copy ¶

Copy copies all key/value pairs in src adding them to dst. When a key in src is already present in dst, the value in dst will be overwritten by the value associated with the key in src.

func DeleteFunc ¶

DeleteFunc deletes any key/value pairs from m for which del returns true.

func Equal ¶

Equal reports whether two maps contain the same key/value pairs. Values are compared using ==.

func EqualFunc ¶

EqualFunc is like Equal, but compares values using eq. Keys are still compared with ==.

func Keys ¶

Keys returns the keys of the map m. The keys will be in an indeterminate order.

func Values ¶

Values returns the values of the map m. The values will be in an indeterminate order.

Source Files ¶

Keyboard shortcuts.

: This menu
: Search site
or : Jump to
or : Canonical URL

IMAGES

  1. Google Maps Assignment

    go map assignment

  2. Analysis of the Principle of Go map

    go map assignment

  3. Go by Example

    go map assignment

  4. Go by Example

    go map assignment

  5. go map 设计与实现

    go map assignment

  6. Overall Assignment 4

    go map assignment

COMMENTS

  1. Go maps in action - The Go Programming Language

    Go provides a built-in map type that implements a hash table. Declaration and initialization. A Go map type looks like this: map[KeyType]ValueType where KeyType may be any type that is comparable (more on this later), and ValueType may be any type at all, including another map! This variable m is a map of string keys to int values: var m map ...

  2. Go Maps Explained: How Key-Value Pairs Are Actually Stored

    The new key-value pair gets stored in this overflow bucket rather than forcing a full grow. A map in Go grows when one of two conditions is met: either there are too many overflow buckets, or the map is overloaded, meaning the load factor is too high. Because of these 2 conditions, there are also two kinds of growth:

  3. Maps - Practical Go Lessons

    What is a map? What is a key, a value? How to create a map. How to insert an entry in a map. How to retrieve an entry from a map. 2 Technical concepts covered. Map type. Key-Value pair. Map entry. Hash table. Time complexity. 3 Why do we need maps? In this section, we will detail how maps are working.

  4. Maps explained: create, add, get, delete · YourBasic Go

    A map (or dictionary) is an unordered collection of key-value pairs, where each key is unique. You create a new map with a make statement or a map literal. The default zero value of a map is nil . A nil map is equivalent to an empty map except that elements can’t be added.

  5. Golang Maps by Example | CalliCoder

    In this article, you learned how to declare and initialize maps, how to add keys to a map, how to retrieve the value associated with a given key in a map, how to check for the existence of a key in a map, how to delete a key from a map, and how to iterate over a map.

  6. maps package - maps - Go Packages

    Package maps defines various functions useful with maps of any type. This package does not have any special handling for non-reflexive keys (keys k where k != k), such as floating-point NaNs.

  7. Learn maps in Golang with practical examples | golangbot.com

    What is a map? A map is a inbuilt data type in Go which is used to store key-value pairs. A practical use case for a map is for storing the currency codes and the corresponding currency names.

  8. A Tour of Go - The Go Programming Language

    Mutating Maps. Insert or update an element in map m: m[key] = elem. Retrieve an element: elem = m[key] Delete an element: delete(m, key) Test that a key is present with a two-value assignment: elem, ok = m[key] If key is in m, ok is true. If not, ok is false. If key is not in the map, then elem is the zero value for the map's element type.

  9. maps package - golang.org/x/exp/maps - Go Packages

    Package maps defines various functions useful with maps of any type.

  10. A Tour of Go - The Go Programming Language

    Maps. A map maps keys to values. The zero value of a map is nil. A nil map has no keys, nor can keys be added. The make function returns a map of the given type, initialized and ready for use. <