setter

package
v1.1.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Sep 26, 2023 License: MIT Imports: 5 Imported by: 0

Documentation

Overview

Package setter allows for manipulation of a value of an exported field of any struct.

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func Set

func Set(strct interface{}, field string, val interface{}) error

Set assigns the value `val` to the field `field` on the struct `strct`. Unexported fields are supported.

Example (ErrFieldDoesNotExists)
package main

import (
	"fmt"

	"github.com/gomponents/gontainer-helpers/setter"
)

func main() {
	person := struct {
		name string
	}{}
	err := setter.Set(&person, "firstname", "Mary")
	fmt.Println(err)
}
Output:

set `*struct { name string }`."firstname": field `firstname` does not exist
Example (ErrNoPtr)
package main

import (
	"fmt"

	"github.com/gomponents/gontainer-helpers/setter"
)

func main() {
	person := struct {
		name string
	}{}
	err := setter.Set(person, "name", "Mary")
	fmt.Println(err)
}
Output:

expected pointer to struct, struct given
Example (Ok)
package main

import (
	"fmt"

	"github.com/gomponents/gontainer-helpers/setter"
)

func main() {
	person := struct {
		name string
	}{}
	err := setter.Set(&person, "name", "Mary")
	fmt.Println(person.name)
	fmt.Println(err)
}
Output:

Mary
<nil>

Types

This section is empty.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL