Maps (Key-Value Pairs) in Go

List Topics
February 17, 2025
No Comments
5 min read

In Go, a map is a data structure that stores key-value pairs. Each key maps to a specific value, and maps allow fast lookups, additions, and deletions of key-value pairs.

Step-by-Step Explanation:

1️⃣ Declaring and Initializing a Map

In Go, a map is typically created using the make() function, or it can be initialized directly with values.

🔹 Example 1 (Using make()):

Go
package main

import "fmt"

func main() {
    // Creating an empty map
    m := make(map[string]int)

    // Assigning key-value pairs to the map
    m["Alice"] = 25
    m["Bob"] = 30

    fmt.Println(m)  // Output: map[Alice:25 Bob:30]
}

🔹 Output:

Bash
map[Alice:25 Bob:30]

👉 Here, m is a map that holds string keys and int values. We added two key-value pairs to the map.

2️⃣ Accessing Key-Value Pairs

To access a value from a map, you simply use the map name and the key. If the key exists in the map, the associated value is returned. If not, a default value (such as 0 or "") is returned.

🔹 Example:

Go
package main

import "fmt"

func main() {
    m := map[string]int{
        "Alice": 25,
        "Bob":   30,
    }

    // Accessing value for "Alice"
    age := m["Alice"]
    fmt.Println(age)  // Output: 25

    // If key "Eve" does not exist, returns default value
    age2 := m["Eve"]
    fmt.Println(age2)  // Output: 0
}

🔹 Output:

Bash
25
0

👉 Here, the value for "Alice" is returned, while the key "Eve" is not found, so the default value 0 is returned.

3️⃣ Deleting Key-Value Pairs

You can remove a specific key-value pair from a map using the delete() function.

🔹 Example:

Go
package main

import "fmt"

func main() {
    m := map[string]int{
        "Alice": 25,
        "Bob":   30,
    }

    // Deleting the key-value pair for "Alice"
    delete(m, "Alice")

    fmt.Println(m)  // Output: map[Bob:30]
}

🔹 Output:

Bash
map[Bob:30]

👉 Here, the key-value pair "Alice": 25 is removed, leaving only "Bob": 30 in the map.

4️⃣ Checking if a Key Exists

In Go, you can check if a key exists in a map using the comma ok syntax. If the key exists, it returns true; otherwise, it returns false.

🔹 Example:

Go
package main

import "fmt"

func main() {
    m := map[string]int{
        "Alice": 25,
        "Bob":   30,
    }

    age, ok := m["Alice"]
    fmt.Println(age, ok)  // Output: 25 true

    age2, ok2 := m["Eve"]
    fmt.Println(age2, ok2)  // Output: 0 false
}

🔹 Output:

Bash
25 true
0 false

👉 Here, the first lookup for "Alice" returns true because it exists in the map, while the second lookup for "Eve" returns false because the key is not found.

5️⃣ Length of a Map

You can get the number of key-value pairs in a map using the len() function.

🔹 Example:

Go
package main

import "fmt"

func main() {
    m := map[string]int{
        "Alice": 25,
        "Bob":   30,
    }

    fmt.Println(len(m))  // Output: 2
}

🔹 Output:

Bash
2

👉 Here, len(m) returns the number of key-value pairs in the map, which is 2.

6️⃣ Using Different Types for Keys and Values

Go allows you to use any type of data as a key or value, such as string, int, struct, or even another map.

🔹 Example:

Go
package main

import "fmt"

func main() {
    // Using slices as keys and integers as values
    m := map[[]int]int{
        {1, 2, 3}: 100,
        {4, 5, 6}: 200,
    }

    fmt.Println(m)  // Output: map[[1 2 3]:100 [4 5 6]:200]
}

🔹 Output:

Bash
map[[1 2 3]:100 [4 5 6]:200]

👉 Here, we used slices ([]int) as keys and integers (int) as values in the map.

Summary:

  • A map is a key-value data structure in Go that allows for fast lookups, additions, and deletions of key-value pairs.
  • You can use make() to create a new map.
  • The delete() function is used to remove a key-value pair from a map.
  • len() returns the number of key-value pairs in a map.
  • comma ok syntax helps check whether a key exists in the map.

This gives you a fundamental understanding of how to work with maps in Go.


গো-তে ম্যাপ (কী-ভ্যালু পেয়ার)

গো-তে ম্যাপ হল একটি ডেটা স্ট্রাকচার যা কী-ভ্যালু পেয়ার ধারণ করে। প্রতিটি কী একটি একক উপাদানকে চিহ্নিত করে এবং তার সাথে সম্পর্কিত একটি ভ্যালু থাকে। এই ম্যাপের মাধ্যমে আপনি দ্রুত কোনো মান অনুসন্ধান, যোগ করা বা মুছে ফেলার কাজ করতে পারেন।

ধাপে ধাপে ব্যাখ্যা:

1️⃣ ম্যাপ ডিক্লেয়ারেশন এবং ইনিশিয়ালাইজেশন

গো-তে ম্যাপ ডিক্লেয়ার করার জন্য make() ফাংশন ব্যবহার করা হয়, অথবা একে আভ্যন্তরীণভাবে ডিক্লেয়ার করে ইনিশিয়ালাইজ করা যায়।

🔹 উদাহরণ ১ (make() ব্যবহার):

Go
package main

import "fmt"

func main() {
    // একটি খালি ম্যাপ তৈরি করা
    m := make(map[string]int)

    // ম্যাপের মধ্যে কী-ভ্যালু পেয়ার অ্যাসাইন করা
    m["Alice"] = 25
    m["Bob"] = 30

    fmt.Println(m)  // আউটপুট: map[Alice:25 Bob:30]
}

🔹 আউটপুট:

Bash
map[Alice:25 Bob:30]

👉 এখানে, m হলো একটি ম্যাপ যা string কী এবং int ভ্যালু ধারণ করে। আমরা দুটি কী-ভ্যালু পেয়ার ম্যাপে যোগ করেছি।

2️⃣ কী-ভ্যালু পেয়ার অ্যাক্সেস করা

ম্যাপ থেকে একটি ভ্যালু এক্সেস করতে, আপনি কেবল ম্যাপের নাম এবং কী ব্যবহার করেন। যদি কীটি ম্যাপে থাকে, তবে তার সংশ্লিষ্ট ভ্যালু রিটার্ন হবে। যদি না থাকে, তবে একটি ডিফল্ট মান (যেমন, 0 বা "") রিটার্ন হবে।

🔹 উদাহরণ:

Go
package main

import "fmt"

func main() {
    m := map[string]int{
        "Alice": 25,
        "Bob":   30,
    }

    // "Alice"-এর ভ্যালু অ্যাক্সেস করা
    age := m["Alice"]
    fmt.Println(age)  // আউটপুট: 25

    // "Eve"-এর জন্য কোন ভ্যালু না থাকলে ডিফল্ট মান রিটার্ন করবে
    age2 := m["Eve"]
    fmt.Println(age2)  // আউটপুট: 0
}

🔹 আউটপুট:

Bash
25
0

👉 এখানে, "Alice" এর ভ্যালু রিটার্ন হয়েছে, কিন্তু "Eve" কীটি ম্যাপে না থাকায় ডিফল্ট মান 0 রিটার্ন হয়েছে।

3️⃣ কী-ভ্যালু পেয়ার মুছে ফেলা

গো-তে delete() ফাংশন ব্যবহার করে আপনি একটি নির্দিষ্ট কী-ভ্যালু পেয়ার মুছে ফেলতে পারেন।

🔹 উদাহরণ:

Go
package main

import "fmt"

func main() {
    m := map[string]int{
        "Alice": 25,
        "Bob":   30,
    }

    // "Alice" কী-ভ্যালু পেয়ার মুছে ফেলা
    delete(m, "Alice")

    fmt.Println(m)  // আউটপুট: map[Bob:30]
}

🔹 আউটপুট:

Bash
map[Bob:30]

👉 এখানে, "Alice" কী-ভ্যালু পেয়ারটি মুছে ফেলা হয়েছে, এবং এখন শুধুমাত্র "Bob" কীটি সিস্টেমে রয়েছে।

4️⃣ ম্যাপের মধ্যে কী পাওয়া যাচ্ছিল কিনা চেক করা

গো-তে, আপনি চেক করতে পারেন যে কোনো কী একটি ম্যাপে রয়েছে কিনা। এটি comma ok সিনট্যাক্স ব্যবহার করে করা হয়। যদি কীটি ম্যাপে থাকে, তবে এটি true রিটার্ন করবে, অন্যথায় false রিটার্ন করবে।

🔹 উদাহরণ:

Go
package main

import "fmt"

func main() {
    m := map[string]int{
        "Alice": 25,
        "Bob":   30,
    }

    age, ok := m["Alice"]
    fmt.Println(age, ok)  // আউটপুট: 25 true

    age2, ok2 := m["Eve"]
    fmt.Println(age2, ok2)  // আউটপুট: 0 false
}

🔹 আউটপুট:

Bash
25 true
0 false

👉 এখানে, প্রথমে "Alice" কীটি উপস্থিত ছিল, তাই true রিটার্ন করেছে, কিন্তু "Eve" কীটি উপস্থিত না থাকায় false রিটার্ন হয়েছে।

5️⃣ ম্যাপের লেন্থ (Length)

ম্যাপের মধ্যে মোট কী-ভ্যালু পেয়ার গোনা যায় len() ফাংশন ব্যবহার করে।

🔹 উদাহরণ:

Go
package main

import "fmt"

func main() {
    m := map[string]int{
        "Alice": 25,
        "Bob":   30,
    }

    fmt.Println(len(m))  // আউটপুট: 2
}

🔹 আউটপুট:

Bash
2

👉 এখানে, len(m) ফাংশন দুটি কী-ভ্যালু পেয়ার গুনে 2 রিটার্ন করেছে।

6️⃣ অ্যাম্বিগুয়াস কী এবং ভ্যালু টাইপ

গো-তে আপনি যেকোনো ধরনের ডেটা টাইপ ব্যবহার করতে পারেন কী বা ভ্যালু হিসেবে, যেমন string, int, struct, বা এমনকি অন্য কোনো ম্যাপ।

🔹 উদাহরণ:

Go
package main

import "fmt"

func main() {
    // স্লাইসকে কী হিসেবে এবং ইন্টকে ভ্যালু হিসেবে ব্যবহার করা
    m := map[[]int]int{
        {1, 2, 3}: 100,
        {4, 5, 6}: 200,
    }

    fmt.Println(m)  // আউটপুট: map[[1 2 3]:100 [4 5 6]:200]
}

🔹 আউটপুট:

Bash
map[[1 2 3]:100 [4 5 6]:200]

👉 এখানে, স্লাইস []int কী হিসেবে ব্যবহৃত হয়েছে এবং ইন্ট (int) ভ্যালু হিসেবে ব্যবহৃত হয়েছে।

সারাংশ:

  • ম্যাপ: এটি একটি কী-ভ্যালু পেয়ার ধারণকারী ডেটা স্ট্রাকচার, যা দ্রুত ডেটা অনুসন্ধান এবং সংরক্ষণ করতে সাহায্য করে।
  • make(): নতুন ম্যাপ তৈরি করতে ব্যবহার করা হয়।
  • delete(): কোনো কী-ভ্যালু পেয়ার মুছে ফেলতে ব্যবহার করা হয়।
  • len(): ম্যাপে মোট কী-ভ্যালু পেয়ার গোনা যায়।
  • comma ok: কী-ভ্যালু পাওয়া যাচ্ছে কিনা চেক করার জন্য ব্যবহৃত হয়।

এটি আপনাকে গো-তে ম্যাপ ব্যবহারের মৌলিক ধারণা ও কাজ করার পদ্ধতি সম্পর্কে ধারণা দেয়।

©2025 Linux Bangla | Developed & Maintaind by Linux Bangla.