NetLife Guru

Open source Go packages for fast, maintainable web systems. Built with a documentation-first approach.

Product
OverviewGolang packagesNews
Documentation
DocumentationGo LoggerGo RouterGo DB Form
Company
OverviewContactNewsGitHub
Community / Support
Supportinfo@netlife.guru
© 2026 NetLife Guru. All rights reserved.
GitHubinfo@netlife.guru
NetLife GuruNetLife GuruNetLife Guru
NetLife GuruNetLife GuruNetLife Guru
OverviewDocumentationNewsSupportContact

Golang packages

About
ScanStructRowsScanStructSliceScanStructOneScanMapRowsFillFromMap
MappingDynamic RowsEdge Cases
MapperReal Usage

FillFromMap

Use FillFromMap to fill a Go struct from an existing map.

Use FillFromMap when you already have a map[string]any and want to fill a Go struct from it.

This is useful when data does not come directly from database rows, or when you want to reuse mapper’s field matching and assignment logic with map-based data.

Typical use cases include:

  • converting dynamic row maps into structs
  • processing data from ScanMapRows
  • mapping decoded JSON-like values
  • mapping data from generic pipelines
  • converting test fixtures into structs
  • manually preparing row-like data before validation

Complete Usage Example

This example creates a map[string]any manually and fills a User struct from it.

package main

import (
	"fmt"
	"log"
	"time"

	"github.com/netlifeguru/mapper"
)

type User struct {
	ID        int64     `db:"id"`
	Name      string    `db:"name"`
	Email     string    `db:"email"`
	Active    bool      `db:"active"`
	CreatedAt time.Time `db:"created_at"`
}

func main() {
	row := map[string]any{
		"id":         1,
		"name":       "John Doe",
		"email":      "john@example.com",
		"active":     true,
		"created_at": time.Date(2026, 5, 7, 11, 15, 21, 0, time.UTC),
	}

	var user User

	err := mapper.FillFromMap(&user, row)
	if err != nil {
		log.Fatal(err)
	}

	fmt.Printf(
		"ID: %d | Name: %s | Email: %s | Active: %t | Created: %s\n",
		user.ID,
		user.Name,
		user.Email,
		user.Active,
		user.CreatedAt.Format("2006-01-02 15:04:05"),
	)
}

Basic Idea

FillFromMap assigns values from a map into matching struct fields.

err := mapper.FillFromMap(&user, row)
if err != nil {
	return err
}

The destination must be a non-nil pointer to a struct.

Define a Struct

Create a struct that describes the expected data shape.

type User struct {
	ID        int64     `db:"id"`
	Name      string    `db:"name"`
	Email     string    `db:"email"`
	Active    bool      `db:"active"`
	CreatedAt time.Time `db:"created_at"`
}

Mapper uses the same field matching rules as struct scanning:

  1. db tag
  2. json tag
  3. Go field name
  4. snake_case fallback

Prepare a Map

Create or receive a map with column-like keys.

row := map[string]any{
	"id":         1,
	"name":       "John Doe",
	"email":      "john@example.com",
	"active":     true,
	"created_at": time.Date(2026, 5, 7, 11, 15, 21, 0, time.UTC),
}

The map keys should match the struct tags or field names.

Fill the Struct

Pass a pointer to the destination struct.

var user User

err := mapper.FillFromMap(&user, row)
if err != nil {
	return err
}

After mapping, the struct contains the assigned values.

fmt.Println(user.ID, user.Name, user.Email, user.Active)

Extra Map Keys

Extra keys are ignored when no matching struct field exists.

type User struct {
	ID   string `db:"id"`
	Name string `db:"name"`
}
row := map[string]any{
	"id":         "u_123",
	"name":       "Alice",
	"created_at": "2026-05-22",
}

The created_at key is ignored because the struct does not define a matching field.

Missing Map Keys

If a map does not contain a key for a struct field, that field keeps its zero value.

type User struct {
	ID     string `db:"id"`
	Name   string `db:"name"`
	Active bool   `db:"active"`
}
row := map[string]any{
	"id":   "u_123",
	"name": "Alice",
}

The Active field remains false.

Pointer Fields

Pointer fields are supported.

type User struct {
	ID    string  `db:"id"`
	Email *string `db:"email"`
}
row := map[string]any{
	"id":    "u_123",
	"email": "alice@example.com",
}

When the value is present and not nil, mapper allocates and assigns the pointer.

var user User

err := mapper.FillFromMap(&user, row)
if err != nil {
	return err
}

if user.Email != nil {
	fmt.Println(*user.Email)
}

If the map value is nil, the pointer remains nil.

JSON Fields

FillFromMap can assign JSON strings or byte slices into slice and map fields.

type User struct {
	ID       string            `db:"id"`
	Tags     []string          `db:"tags"`
	Metadata map[string]string `db:"metadata"`
}
row := map[string]any{
	"id":       "u_123",
	"tags":     `["admin","active"]`,
	"metadata": `{"source":"import","role":"admin"}`,
}
var user User

err := mapper.FillFromMap(&user, row)
if err != nil {
	return err
}

If the JSON is invalid, mapper returns an error.

Invalid Destination

The destination must be a non-nil pointer to a struct.

Correct:

var user User

err := mapper.FillFromMap(&user, row)

Incorrect:

// Wrong: destination is not a pointer.
err := mapper.FillFromMap(user, row)

Incorrect:

var user *User

// Wrong: destination is nil.
err := mapper.FillFromMap(user, row)

When to Use FillFromMap

Use FillFromMap when:

  • you already have map[string]any
  • you want mapper’s struct field matching without scanning rows
  • you need to convert dynamic data into a typed struct
  • you want to reuse db and json tags
  • you want assignment support for pointers, nullable values, slices, maps, and JSON fields

When Not to Use It

Do not use FillFromMap when you already have database rows and simply want structs.

Use struct scanning directly.

users, err := mapper.ScanStructSlice[User](rows)

or:

err := mapper.ScanStructRows[User](rows, func(user *User) error {
	users = append(users, *user)
	return nil
})

Related Example

A standalone example is available in the examples repository:

FillFromMap example

ScanMapRows

Use ScanMapRows to process database rows as dynamic map values.

Scanning

Scan database rows into Go structs using ScanStructRows, ScanStructSlice, and ScanStructOne.

On this page

Complete Usage ExampleBasic IdeaDefine a StructPrepare a MapFill the StructExtra Map KeysMissing Map KeysPointer FieldsJSON FieldsInvalid DestinationWhen to Use FillFromMapWhen Not to Use ItRelated Example