项目作者: RebirthLee

项目描述 :
golang initialize or new struct with default value
高级语言: Go
项目地址: git://github.com/RebirthLee/golang-default.git
创建时间: 2020-04-08T09:26:54Z
项目社区:https://github.com/RebirthLee/golang-default

开源协议:Other

下载


golang-default

Build Status
GitHub release (latest by date)
GitHub go.mod Go version
license

Initialize or New Struct with default value

Setup

  1. go get github.com/rebirthlee/golang-default

Support type

  • int/8/16/32/64, uint/8/16/32/64
    1. `def:"100"`
    2. `def:"-100"` // support only signed integer
    3. // support hexadecimal, octal, binary
    4. `def:"0xff"` // hexadecimal
    5. `def:"0xFF"` // hexadecimal
    6. `def:"0o77"` // octal
    7. `def:"0b11001111"` // binary
  • float32/64
    1. `def:"3.141592653589"`
    2. `def:"-3.141592"`
  • complex64/128

    1. // `def:"{real part},{imaginary part}"`
    2. `def:"3.14,-10"`
    3. `def:"-3.14,3"`
  • time.Duration

    1. // calling time.ParseDuration
    2. `def:"1h"` // 1 * time.Hour
  • time.Time

    1. `def:"now"` // time.Now()
    2. `def:"+1h"` // time.Now().Add(1 * time.Hour)
    3. `def:"-1h"` // time.Now().Add(-1 * time.Hour)
  • Nested Struct
    ``go type Parent struct { Name stringdef:”Parent Struct”OneChild Childdef:”dive”TwoChild Childdef:”dive”`
    }

type Child struct {
Key string def:"unknown"
Number int def:"-1"
}

  1. - Pointer of type
  2. ```go
  3. type Parent struct {
  4. Name *string `def:"Parent Struct"`
  5. OneChild *Child `def:"dive"`
  6. TwoChild *Child `def:"dive"`
  7. }
  8. type Child struct {
  9. Key *string `def:"unknown"`
  10. Number *int `def:"-1"`
  11. }
  • Array

    1. type Sample struct {
    2. Arr [3]int `def:"dive,-1"` // [-1, -1, -1]
    3. }
  • Slice
    ``go type Sample struct { //def:”dive({length},{capacity(optional)}),{value}”Sli []intdef:”dive(5),-1”` // [-1, -1, -1, -1, -1]

    //cap(SliWithCap) == 7
    SliWithCap []NestedSample def:"dive(3,7),dive" // [{nested struct},{nested struct},{nested struct}]
    }

type NestedSample struct {
Name string def:"nested struct"
}

  1. - Map
  2. ```go
  3. type Sample struct {
  4. DiveMap map[string]*Struct `def:"dive{\"john doe\":dive,\"some one\":dive,\"key\":dive}"`
  5. /*
  6. {
  7. "john doe": &{who?},
  8. "some one": &{who?},
  9. "key": &{who?}
  10. }
  11. */
  12. StructKeyMap map[*Struct]bool `def:"dive{dive:true,dive:false,dive:true}"`
  13. /*
  14. {
  15. &{who?}: true,
  16. &{who?}: false,
  17. &{who?}: true
  18. }
  19. */
  20. DiveNestedMap map[string]map[*Struct]bool `def:"dive{\"key1\":dive{dive:true,dive:false},\"key2\":dive{dive:false,dive:false}}"`
  21. /*
  22. {
  23. "key1": {
  24. &{who?}: true,
  25. &{who?}: false
  26. },
  27. "key2": {
  28. &{who?}: false,
  29. &{who?}: false
  30. }
  31. }
  32. */
  33. }
  34. Struct struct {
  35. Name string `def:"who?"`
  36. }
  • Json
    ``go type Sample struct { Arr [3]intdef:”[1,2,3]”// [1,2,3] Sli []stringdef:”[\”slice 1\”,\”slice 2\”]”// [slice 1,slice 2] Map map[string]interface{}def:”{\”key1\”:123,\”key2\”:\”value\”,\”nested map\”:{\”key\”:\”val\”}}”`
    /*
    {

    1. "key1":123,
    2. "key2":"value",
    3. "nested map":{
    4. "key":"val"
    5. }

    }
    */

    Nested NestedSample def:"{\"displayName\":\"nested struct type\"}" // {nested struct type}
    PtrNested *NestedSample def:"{\"displayName\":\"nested struct pointer type\"}" // &{nested struct pointer type}
    }

type NestedSample struct {
Name string json:"displayName"
}

  1. - Function
  2. [Example](/example/func/main.go)
  3. ## Usage
  4. ### Simple
  5. ```go
  6. import (
  7. "fmt"
  8. "github.com/rebirthlee/golang-default"
  9. )
  10. type Person struct {
  11. Age int `def:"20"`
  12. Name string `def:"hellp"`
  13. }
  14. ...
  15. var p Person
  16. if err := def.Init(&p); err != nil {
  17. // error handle
  18. }
  19. fmt.Println(p) //out: {20 hellp}

Init

If you got error, the next field of struct will not be initialized.

  1. if err := def.Init(&p); err != nil {
  2. // error handle
  3. }

JustInit

Even though it has an error, It will try to initialize all fields.
And you can know that error field of struct.

  1. if err := def.JustInit(&p); err != nil {
  2. justErr := err.(*def.ErrorJustInit)
  3. fmt.Println(justErr.Error())
  4. // error handle
  5. }

MustInit

It isn’t return error. but it will be panic when you has an error.

  1. def.MustInit(&p) // hasn't return error.

New

If you got error, it will be return nil.

  1. i, err := def.New(Person{})
  2. if err != nil {
  3. // error handle
  4. } else {
  5. p := i.(*Person)
  6. fmt.Println(p) //out: &{20 hellp}
  7. }

JustNew

Even though it has an error, It must return pointer of struct with error.

  1. i, err := def.JustNew(Person{})
  2. if err != nil {
  3. justErr := err.(*def.ErrorJustInit)
  4. fmt.Println(justErr.Error())
  5. // error handle
  6. }
  7. p := i.(*Person)
  8. fmt.Println(p) //out: &{20 hellp}

MustNew

It isn’t return error. but it will be panic when you has an error.

  1. p := def.MustNew(Person{}).(*Person) // hasn't return error.
  2. fmt.Println(p) //out: &{20 hellp}

License

THE BEER-WARE LICENSE (Revision 42)