Welcome to collectivesolver - Programming & Software Q&A with code examples. A website with trusted programming answers. All programs are tested and work.

Contact: aviboots(AT)netvision.net.il

Semrush - keyword research tool

Create your online store today with Shopify

Turn ChatGPT, Claude, Gemini, And CoPilot Into Your Personal Assistant, Business Coach, Content Creator, And More

AFFILIATE MARKETING Your all-in-one performance engine Manage affiliates, creators, and customer referrals in one unified platform—turning every partnership into measurable growth

Secure & Reliable Web Hosting, Free Domain, Free SSL, 1-Click WordPress Install, Expert 24/7 Support

Disclosure: My content contains affiliate links.

43,239 questions

56,142 answers

573 users

How to combine 2 maps into a third map in Go

1 Answer

0 votes
package main

import (
    "fmt"
)

func main() {
    map1 := map[string]string{
        "A": "aaa",
        "B": "bbb",
    }

    map2 := map[string]string{
        "C": "ccc",
        "B": "XYZ", // Overwrites key "B"
        "D": "ddd",
    }

    combined := make(map[string]string)

    // Copy map1 into combined
    for k, v := range map1 {
        combined[k] = v
    }

    // Merge map2 into combined (overwrites duplicates)
    for k, v := range map2 {
        combined[k] = v
    }

    // Display the result
    for k, v := range combined {
        fmt.Printf("%s => %s\n", k, v)
    }
}



/*
run:

D => ddd
C => ccc
A => aaa
B => XYZ

*/

 



answered Aug 26, 2025 by avibootz
edited Aug 26, 2025 by avibootz

Related questions

1 answer 154 views
1 answer 129 views
129 views asked Aug 7, 2025 by avibootz
2 answers 191 views
1 answer 271 views
2 answers 196 views
...