Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Rohan Verma edited this pageDec 10, 2019 ·2 revisions

Welcome to the koanf wiki!

Tips and Tricks

Mapstructure

Koanf uses the mapstructure package that's used for decoding the config from koanf to structs.

Default configuration

By default, koanf uses the following mapstructure decoder config:

mapstructure.DecoderConfig{...WeaklyTypedInput:true,TagName="koanf"...}

Weakly typed input

WeaklyTypedInput is set to be true and implies that koanf will do the following "weak" conversions:

  • bools to string (true = "1", false = "0")
  • numbers to string (base 10)
  • bools to int/uint (true = 1, false = 0)
  • strings to int/uint (base implied by prefix)
  • int to bool (true if value != 0)
  • string to bool (accepts: 1, t, T, TRUE, true, True, 0, f, F, false, False. Anything else is an error)
  • empty array = empty map and vice versa
  • negative numbers to overflowed uint values (base 10)
  • slice of maps to a merged map
  • single values are converted to slices if required. Each is weakly decoded. For example: "4" can become []int{4} the target type is an int slice.

Struct Tag

TheTagName inDecoderConfig is set tokoanf by default. If it is not set, the default fallback ismapstructure. Although, for fields without any tag,mapstructure fallsback to the field name. ref:#5

Embedded structs

For embedded/composite structs, you can use the,squash tag to squash multiple embedded structs. You can refer to an example present in themapstructure godoc, ref:#8


[8]ページ先頭

©2009-2025 Movatter.jp