duration

package module
v1.1.0 Latest Latest
Warning

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

Go to latest
Published: Sep 2, 2024 License: MIT Imports: 2 Imported by: 0

README

duration

duration Marshaler and UnMarshaler support

Usage

Marshaler

type Person struct {
	Name    string            `json:"name"`
	Created duration.Duration `json:"created"`
}

func main() {
	p := Person{
		Name:    "John Doe",
		Created: duration.Second,
	}
	marshal, err := json.Marshal(p)
	if err != nil {
		panic(err)
	}
	fmt.Println(string(marshal))
}

output

{"name":"John Doe","created":"1s"}

UnMarshaler

type Person struct {
	Name    string            `json:"name"`
	Created duration.Duration `json:"created"`
}

func main() {
	var p Person
	str := `{"name":"John Doe","created":"1s"}`
	err := json.Unmarshal([]byte(str), &p)
	if err != nil {
		panic(err)
	}
	fmt.Println(p)
}

output

{John Doe 1s}

Documentation

Index

Constants

View Source
const (
	Nanosecond  = Duration(time.Nanosecond)
	Microsecond = Duration(time.Microsecond)
	Millisecond = Duration(time.Millisecond)
	Second      = Duration(time.Second)
	Minute      = Duration(time.Minute)
	Hour        = Duration(time.Hour)
)

Variables

This section is empty.

Functions

This section is empty.

Types

type Duration

type Duration time.Duration

Duration is a wrapper of time.Duration, which implements TextUnmarshaler, TextMarshaler, MarshalJSON()

func From

func From(duration time.Duration) Duration

func (*Duration) Duration

func (d *Duration) Duration() time.Duration

func (Duration) MarshalJSON

func (d Duration) MarshalJSON() ([]byte, error)

func (Duration) MarshalText

func (d Duration) MarshalText() (text []byte, err error)

func (Duration) String added in v1.1.0

func (d Duration) String() string

func (*Duration) UnmarshalText

func (d *Duration) UnmarshalText(text []byte) error

Jump to

Keyboard shortcuts

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