Skip to content
This repository was archived by the owner on Jul 22, 2024. It is now read-only.

Commit c76ad0d

Browse files
committed
add required example
1 parent d417b47 commit c76ad0d

File tree

1 file changed

+24
-0
lines changed

1 file changed

+24
-0
lines changed

mapstructure_examples_test.go

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -254,3 +254,27 @@ func ExampleDecode_omitempty() {
254254
// Output:
255255
// &map[Age:0 FirstName:Somebody]
256256
}
257+
258+
func ExampleDecode_requiredField() {
259+
// The required tag can indicate which fields in the struct are mandatory.
260+
type Person struct {
261+
FirstName string `mapstructure:",required"`
262+
LastName string `mapstructure:",required"`
263+
}
264+
265+
input := map[string]interface{}{
266+
"LastName": "Hashimoto",
267+
}
268+
269+
var result Person
270+
err := Decode(input, &result)
271+
if err == nil {
272+
panic("should have an error")
273+
}
274+
275+
fmt.Println(err.Error())
276+
// Output:
277+
// 1 error(s) decoding:
278+
279+
// * '' is missing required key: FirstName
280+
}

0 commit comments

Comments
 (0)