Introduction to Hash Table in Go Programming

Introduction to Hash Tables In Go programming

Introduction

A hash table is a data structure used in a lot of different languages, and it’s a very useful data structure, it allows you fast access to large bodies of data. Hash table generally contains a key-value pair, so there are a lot of values inside this hash table but each value is associated with a unique key.

So, for instance, social security numbers and emails might be the unique key of the person and so that might be the key and then the value might be the email address of the person. So that’s a pair. so, unique key and value and the key have to be unique that’s actually completely important.

for example, GPS coordinates and addresses. if every address has a unique set of GPS coordinates associated with it. then GPS coordinates use as the key and then the address might be your house address, which may or may not be unique.

How does it work?

The hash table means to store these key-value pairs, and it is important that each key is unique. So a hash function defines and its uses to take a key and compute a slot in the hash table to insert the value according to the key.

It has a lot of slots where you can put values in there. Now which slot is selected to puts the value in is base on the key. A hash function uses to process the key and generate the number of the slot that you want to insert the value into.

The hash function is a function that takes as its argument the key and it returns the slot where you want to put the value. actually, you never call this hash function explicitly. This is something that goes on behind the scenes inside the Go language to understand there is a hash function that does this.

So, here’s we have the following example of the hash table. and look at the hash table key-value pair inside it with unique keys.

hash table key-value pair

In the below table, they’re all unique keys, their strings are unique, and there are some values A, B, and C, they’re arbitrary. So John is associated with A, Peter associated with B, Robin associated with C, and these three key-value pairs into my hash table.

Actually, we have some array and the values A, B, and C are all placed in different slots inside this structure.

The hash function takes the key and it determines which slot it is going to place the corresponding valuing in.

Advantages

1. fast lookup table than lists.

So, this is an advantage compared to like a list that you’d find in another language. Faster lookup in lists because it is linear time lookup. it means if you want to find an element in a list. you have to start at the beginning of the list and go to the next and keep comparing until you find the one you want, find one that matches. the linear time, the longer the list is the longer it takes to find things in the list on average.

Simply, you take the index and key rather, you use the hash function. it takes some constant amount of time and it gives you the index and you go straight to that index.

2. Arbitrary key

It is better than slices or arrays where you would have to use integers. You can use arbitrary keys and those keys can have some sort of meaning to them. So, the disadvantages are that you may have what is called conflict inside your hash table. So conflict is when two keys hash at the same slot.

Now, there are ways to handle this conflict. You can put them both in there and put them in a linked list or something like this but, when you get conflict, the speed gets a little slower.

So, you don’t have to worry about how they handle. that’s built into Golang, but that can slow you down but, that conflict is rare because of the hash function made in such a way that conflict is very rare.

Leave a Reply

Your email address will not be published. Required fields are marked *