Documentation
¶
Overview ¶
package conv
Index ¶
Examples ¶
Constants ¶
This section is empty.
Variables ¶
View Source
var TimeLayout = "Mon Jan 2 15:04:05 -0700 MST 2006"
TimeLayout default time layout for convert To time.Time
Functions ¶
func To ¶
func To(src, dst interface{}) error
To convert to src to dst
Example ¶
viper.SetConfigType("toml")
cfgData := []byte(`
Num = 21
String = "String"
Duration = "100ms"
Time = "Fri Nov 1 19:13:55 +0800 CST 2019"
ByteSize = "100MB"
IP = "8.8.8.8"
MAC = "02:00:5e:10:00:00:00:01"
URL = "http://host/path?param=x"
Regexp = "[0-9]+"
Mail = "name <[email protected]>"
`)
if err := viper.ReadConfig(bytes.NewBuffer(cfgData)); err != nil {
panic(err)
}
rawCfg := viper.AllSettings()
var cfg struct {
Num int
String string
Duration time.Duration
Time *time.Time
ByteSize conv.ByteSize
IP net.IP
MAC net.HardwareAddr
URL *url.URL
Regexp *regexp.Regexp
Mail *mail.Address
}
if err := conv.To(rawCfg, &cfg); err != nil {
panic(err)
}
fmt.Printf("%v\n", cfg)
Output: {21 String 100ms 2019-11-01 19:13:55 +0800 CST 104857600 8.8.8.8 02:00:5e:10:00:00:00:01 http://host/path?param=x [0-9]+ "name" <[email protected]>}
Types ¶
type CannotConvError ¶
type CannotConvError struct {
// contains filtered or unexported fields
}
CannotConvError ...
func (*CannotConvError) Error ¶
func (e *CannotConvError) Error() string
type CannotSetError ¶
type CannotSetError struct{}
func (*CannotSetError) Error ¶
func (e *CannotSetError) Error() string
type OverflowError ¶
type OverflowError struct {
// contains filtered or unexported fields
}
OverflowError ...
func (*OverflowError) Error ¶
func (e *OverflowError) Error() string
Click to show internal directories.
Click to hide internal directories.