# How to write the two dimensional map to the file?

**URL:** <https://forum.golangbridge.org/t/how-to-write-the-two-dimensional-map-to-the-file/21115>\
**Category:** Code Review\
**Created:** [October 27, 2020, 1:47pm UTC](https://forum.golangbridge.org/t/how-to-write-the-two-dimensional-map-to-the-file/21115 "2020-10-27T13:47:23Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Yo\_94](https://forum.golangbridge.org/letter_avatar_proxy/v4/letter/y/e274bd/32.png) [@Yo\_94](https://forum.golangbridge.org/u/Yo_94)\
**Post date:** [October 27, 2020, 1:47pm UTC](https://forum.golangbridge.org/t/how-to-write-the-two-dimensional-map-to-the-file/21115/1 "2020-10-27T13:47:23Z")

</div>

I have a two-dimensional map that I am trying to write on an output file using Encoding/Gob library but I am getting the following error: “gob: type not registered for interface: map[string]map[int]example1Obj”

This is the code:

This is the code:  
gob.Register(map[string]interface{}{})  
fileName := os.Args[1]  
File, err := os.Create(fileName)  
if err != nil {  
log.Fatal(err)  
}  
defer File.Close()

```
enc:= gob.NewEncoder(File)

err = enc.Encode(Map.Map_1)
if err != nil {

  fmt.Print(err)
} else {

    File.Sync()
}
```

---

<div class="post-metadata">

**Author:** ![clbanning](https://forum.golangbridge.org/letter_avatar_proxy/v4/letter/c/eb8c5e/32.png) [@clbanning](https://forum.golangbridge.org/u/clbanning)\
**Post date:** [October 27, 2020, 3:49pm UTC](https://forum.golangbridge.org/t/how-to-write-the-two-dimensional-map-to-the-file/21115/2 "2020-10-27T15:49:23Z")

</div>

```auto
package main

import (
	"encoding/gob"
	"fmt"
	"os"
)

func main() {
	obj1 := struct {
		Name string
		Place string
	}{
		Name: "obj1",
		Place: "first"}

	data := map[string]interface{}{
		"x_one": map[int]interface{}{
			1: obj1}}
	gob.Register(data["x_one"])
	gob.Register(data["x_one"].(map[int]interface{})[1])

	f, err := os.Create("2dimmap.gob")
	if err != nil {
		fmt.Println("os.Create err:", err)
		return
	}
	defer f.Close()

	e := gob.NewEncoder(f)

	if err = e.Encode(data); err != nil {
		fmt.Println("e.Encode err:", err)
		return
	}
}

```

---

<div class="post-metadata">

**Author:** ![Yo\_94](https://forum.golangbridge.org/letter_avatar_proxy/v4/letter/y/e274bd/32.png) [@Yo\_94](https://forum.golangbridge.org/u/Yo_94)\
**Post date:** [October 27, 2020, 4:30pm UTC](https://forum.golangbridge.org/t/how-to-write-the-two-dimensional-map-to-the-file/21115/3 "2020-10-27T16:30:54Z")

</div>

So how can I decode it later then?

---

<div class="post-metadata">

**Author:** ![clbanning](https://forum.golangbridge.org/letter_avatar_proxy/v4/letter/c/eb8c5e/32.png) [@clbanning](https://forum.golangbridge.org/u/clbanning)\
**Post date:** [October 28, 2020, 4:43pm UTC](https://forum.golangbridge.org/t/how-to-write-the-two-dimensional-map-to-the-file/21115/5 "2020-10-28T16:43:27Z")

</div>

If I were writing production code and wanted to persist the 2 dimensional map values for subsequent reloading I write them as a struct type that could be parsed back into the map when you need to recover it.

Here’s an example. The persister:

```auto
package main

import (
	"encoding/gob"
	"fmt"
	"os"
)

func main() {
	type Obj1 struct {
		Name string
		Place string
	}

	type Dim struct {
		X string
		Y int
		Obj Obj1
	}

	d := Dim{"", 0, Obj1{"", ""}}
	gob.Register(d)

	// define a 2 dimensional map
	data := map[string]map[int]Obj1{
		"x_one": map[int]Obj1{
			1: {"obj1", "first"}},
		"x_two": map[int]Obj1{
			2: {"obj2", "second"}}}

	f, err := os.Create("2dimmap.gob")
	if err != nil {
		fmt.Println("os.Create err:", err)
		return
	}
	defer f.Close()

	e := gob.NewEncoder(f)

	// for demo; 2 dimensional map values could be persisted
	// using gob encoding as they are created/modified. If encoded
    // and persisted as map values are inserted/updated; then you'll
    // end up with a serialized log file. Then when the 2 dimensional map
    // is restored any updates will be processed, as well.
	for k, v := range data {
		for kk, vv := range v {
			d = Dim{k, kk, vv}
		}
		if err = e.Encode(d); err != nil {
			fmt.Println("e.Encode err:", err)
			return
		}
	}
}

```

Recovering the 2 dimensional map from the backup:

```auto
package main

import (
	"encoding/gob"
	"fmt"
	"io"
	"os"
)

func main() {
	type Obj1 struct {
		Name string
		Place string
	}

	type Dim struct {
		X string
		Y int
		Obj Obj1
	}

	d := Dim{"", 0, Obj1{"", ""}}
	gob.Register(d)

	f, err := os.Open("2dimmap.gob")
	if err != nil {
		fmt.Println("os.Open err:", err)
		return
	}
	defer f.Close()

	dec := gob.NewDecoder(f)

	data := map[string]map[int]Obj1{}
	for {
		if err := dec.Decode(&d); err != nil {
			if err != io.EOF {
				fmt.Println("dec.Decode err:", err)
				return
			}
			break
		}
		y := map[int]Obj1{d.Y: d.Obj}
		data[d.X] = y
	}

	fmt.Printf("%v\n", data)
}

```
