optional: 4d63.com/optional Index | Examples | Files | Directories

package optional

import "4d63.com/optional"

Package optional exports types that wrap the builtin types (int, bool, etc) to represent the lack of value. The types guarantee safety by requiring the developer to unwrap them to get to the inner value. This prevents a nil value being operated on. Optionals marshal to XML and JSON like their underlying type, and omitempty works just like their wrapped type would with a pointer, but without the use of pointers.

These types are an alternative to using pointers, zero values, or similar null wrapper packages. Unlike similar solutions these will omit correctly from XML and JSON without the use of pointers and the compiler will ensure their value is not used when empty.

The package also contains a template that you can use with go generate to create optional types for your own types. See below for instructions on how to use the template.

Examples

Wrap a pointer in an optional:

var i *int = ...
o := optional.OfIntPtr(i)

Unwrap it safely:

o.If(func(i int) {
	// called if o is not empty
})

if _, ok := o.Get(); ok {
	// ok is true if o is not empty
}

Or get it's value with a fallback to a default:

_ := o.ElseZero() // returns the zero value if empty

_ := o.Else(100) // returns 100 if o is empty

_ := o.ElseFunc(func() {
	// called if o is empty
	return 100
})

XML and JSON are supported out of the box. Use `omitempty` to omit the field when the optional is empty:

s := struct {
	Int1 optional.Int `json:"int1,omitempty"`
	Int2 optional.Int `json:"int2,omitempty"`
	Int3 optional.Int `json:"int3,omitempty"`
}{
	Int1: optional.EmptyInt(),
	Int2: optional.OfInt(1000),
	Int3: optional.OfIntPtr(nil),
}

output, _ := json.Marshal(s)

// output = {"int2":1000}

Templates

Use the Optional template for your own types by installing gotemplate.

go get github.com/ncw/gotemplate

Then add a `go generate` comment for your type to any `.go` file in your package.

//go:generate gotemplate "4d63.com/optional/template" OptionalMyType(MyType)

Examples

See the examples for more approaches to use.

Code:

i := 1001
values := []optional.Int{
    optional.EmptyInt(),
    optional.OfInt(1000),
    optional.OfIntPtr(nil),
    optional.OfIntPtr(&i),
}

for _, v := range values {
    fmt.Println(v.Else(1))
}

Output:

1
1000
1
1001

Code:

i := 1001
values := []optional.Int{
    optional.EmptyInt(),
    optional.OfInt(1000),
    optional.OfIntPtr(nil),
    optional.OfIntPtr(&i),
}

for _, v := range values {
    fmt.Println(v.ElseFunc(func() int {
        return 2
    }))
}

Output:

2
1000
2
1001

Code:

i := 1001
values := []optional.Int{
    optional.EmptyInt(),
    optional.OfInt(1000),
    optional.OfIntPtr(nil),
    optional.OfIntPtr(&i),
}

for _, v := range values {
    fmt.Println(v.ElseZero())
}

Output:

0
1000
0
1001

Code:

i := 1001
values := []optional.Int{
    optional.EmptyInt(),
    optional.OfInt(1000),
    optional.OfIntPtr(nil),
    optional.OfIntPtr(&i),
}

for _, v := range values {
    if i, ok := v.Get(); ok {
        fmt.Println(i)
    }
}

Output:

1000
1001

Code:

i := 1001
values := []optional.Int{
    optional.EmptyInt(),
    optional.OfInt(1000),
    optional.OfIntPtr(nil),
    optional.OfIntPtr(&i),
}

for _, v := range values {
    v.If(func(i int) {
        fmt.Println(i)
    })
}

Output:

1000
1001

Code:

s := struct {
    Bool    optional.Bool    `json:"bool"`
    Byte    optional.Byte    `json:"byte"`
    Float32 optional.Float32 `json:"float32"`
    Float64 optional.Float64 `json:"float64"`
    Int16   optional.Int16   `json:"int16"`
    Int32   optional.Int32   `json:"int32"`
    Int64   optional.Int64   `json:"int64"`
    Int     optional.Int     `json:"int"`
    Rune    optional.Rune    `json:"rune"`
    String  optional.String  `json:"string"`
    Time    optional.Time    `json:"time"`
    Uint16  optional.Uint16  `json:"uint16"`
    Uint32  optional.Uint32  `json:"uint32"`
    Uint64  optional.Uint64  `json:"uint64"`
    Uint    optional.Uint    `json:"uint"`
    Uintptr optional.Uintptr `json:"uintptr"`
}{
    Bool:    optional.EmptyBool(),
    Byte:    optional.EmptyByte(),
    Float32: optional.EmptyFloat32(),
    Float64: optional.EmptyFloat64(),
    Int16:   optional.EmptyInt16(),
    Int32:   optional.EmptyInt32(),
    Int64:   optional.EmptyInt64(),
    Int:     optional.EmptyInt(),
    Rune:    optional.EmptyRune(),
    String:  optional.EmptyString(),
    Time:    optional.EmptyTime(),
    Uint16:  optional.EmptyUint16(),
    Uint32:  optional.EmptyUint32(),
    Uint64:  optional.EmptyUint64(),
    Uint:    optional.EmptyUint(),
    Uintptr: optional.EmptyUintptr(),
}

output, _ := json.MarshalIndent(s, "", "  ")
fmt.Println(string(output))

Output:

{
  "bool": false,
  "byte": 0,
  "float32": 0,
  "float64": 0,
  "int16": 0,
  "int32": 0,
  "int64": 0,
  "int": 0,
  "rune": 0,
  "string": "",
  "time": "0001-01-01T00:00:00Z",
  "uint16": 0,
  "uint32": 0,
  "uint64": 0,
  "uint": 0,
  "uintptr": 0
}

Code:

s := struct {
    Bool    optional.Bool    `json:"bool,omitempty"`
    Byte    optional.Byte    `json:"byte,omitempty"`
    Float32 optional.Float32 `json:"float32,omitempty"`
    Float64 optional.Float64 `json:"float64,omitempty"`
    Int16   optional.Int16   `json:"int16,omitempty"`
    Int32   optional.Int32   `json:"int32,omitempty"`
    Int64   optional.Int64   `json:"int64,omitempty"`
    Int     optional.Int     `json:"int,omitempty"`
    Rune    optional.Rune    `json:"rune,omitempty"`
    String  optional.String  `json:"string,omitempty"`
    Time    optional.Time    `json:"time,omitempty"`
    Uint16  optional.Uint16  `json:"uint16,omitempty"`
    Uint32  optional.Uint32  `json:"uint32,omitempty"`
    Uint64  optional.Uint64  `json:"uint64,omitempty"`
    Uint    optional.Uint    `json:"uint,omitempty"`
    Uintptr optional.Uintptr `json:"uintptr,omitempty"`
}{
    Bool:    optional.EmptyBool(),
    Byte:    optional.EmptyByte(),
    Float32: optional.EmptyFloat32(),
    Float64: optional.EmptyFloat64(),
    Int16:   optional.EmptyInt16(),
    Int32:   optional.EmptyInt32(),
    Int64:   optional.EmptyInt64(),
    Int:     optional.EmptyInt(),
    Rune:    optional.EmptyRune(),
    String:  optional.EmptyString(),
    Time:    optional.EmptyTime(),
    Uint16:  optional.EmptyUint16(),
    Uint32:  optional.EmptyUint32(),
    Uint64:  optional.EmptyUint64(),
    Uint:    optional.EmptyUint(),
    Uintptr: optional.EmptyUintptr(),
}

output, _ := json.MarshalIndent(s, "", "  ")
fmt.Println(string(output))

Output:

{}

Code:

s := struct {
    Bool    optional.Bool    `json:"bool"`
    Byte    optional.Byte    `json:"byte"`
    Float32 optional.Float32 `json:"float32"`
    Float64 optional.Float64 `json:"float64"`
    Int16   optional.Int16   `json:"int16"`
    Int32   optional.Int32   `json:"int32"`
    Int64   optional.Int64   `json:"int64"`
    Int     optional.Int     `json:"int"`
    Rune    optional.Rune    `json:"rune"`
    String  optional.String  `json:"string"`
    Time    optional.Time    `json:"time"`
    Uint16  optional.Uint16  `json:"uint16"`
    Uint32  optional.Uint32  `json:"uint32"`
    Uint64  optional.Uint64  `json:"uint64"`
    Uint    optional.Uint    `json:"uint"`
    Uintptr optional.Uintptr `json:"uintptr"`
}{
    Bool:    optional.OfBool(true),
    Byte:    optional.OfByte(1),
    Float32: optional.OfFloat32(2.1),
    Float64: optional.OfFloat64(2.2),
    Int16:   optional.OfInt16(3),
    Int32:   optional.OfInt32(4),
    Int64:   optional.OfInt64(5),
    Int:     optional.OfInt(6),
    Rune:    optional.OfRune(7),
    String:  optional.OfString("string"),
    Time:    optional.OfTime(time.Date(2006, 1, 2, 15, 4, 5, 0, time.UTC)),
    Uint16:  optional.OfUint16(8),
    Uint32:  optional.OfUint32(9),
    Uint64:  optional.OfUint64(10),
    Uint:    optional.OfUint(11),
    Uintptr: optional.OfUintptr(12),
}

output, _ := json.MarshalIndent(s, "", "  ")
fmt.Println(string(output))

Output:

{
  "bool": true,
  "byte": 1,
  "float32": 2.1,
  "float64": 2.2,
  "int16": 3,
  "int32": 4,
  "int64": 5,
  "int": 6,
  "rune": 7,
  "string": "string",
  "time": "2006-01-02T15:04:05Z",
  "uint16": 8,
  "uint32": 9,
  "uint64": 10,
  "uint": 11,
  "uintptr": 12
}

Code:

s := struct {
    Bool    optional.Bool    `json:"bool"`
    Byte    optional.Byte    `json:"byte"`
    Float32 optional.Float32 `json:"float32"`
    Float64 optional.Float64 `json:"float64"`
    Int16   optional.Int16   `json:"int16"`
    Int32   optional.Int32   `json:"int32"`
    Int64   optional.Int64   `json:"int64"`
    Int     optional.Int     `json:"int"`
    Rune    optional.Rune    `json:"rune"`
    String  optional.String  `json:"string"`
    Time    optional.Time    `json:"time"`
    Uint16  optional.Uint16  `json:"uint16"`
    Uint32  optional.Uint32  `json:"uint32"`
    Uint64  optional.Uint64  `json:"uint64"`
    Uint    optional.Uint    `json:"uint"`
    Uintptr optional.Uintptr `json:"uintptr"`
}{}

x := `{}`
json.Unmarshal([]byte(x), &s)
fmt.Println("Bool:", s.Bool.IsPresent())
fmt.Println("Byte:", s.Byte.IsPresent())
fmt.Println("Float32:", s.Float32.IsPresent())
fmt.Println("Float64:", s.Float64.IsPresent())
fmt.Println("Int16:", s.Int16.IsPresent())
fmt.Println("Int32:", s.Int32.IsPresent())
fmt.Println("Int64:", s.Int64.IsPresent())
fmt.Println("Int:", s.Int.IsPresent())
fmt.Println("Rune:", s.Rune.IsPresent())
fmt.Println("String:", s.String.IsPresent())
fmt.Println("Time:", s.Time.IsPresent())
fmt.Println("Uint16:", s.Uint16.IsPresent())
fmt.Println("Uint32:", s.Uint32.IsPresent())
fmt.Println("Uint64:", s.Uint64.IsPresent())
fmt.Println("Uint64:", s.Uint64.IsPresent())
fmt.Println("Uint:", s.Uint.IsPresent())
fmt.Println("Uintptr:", s.Uint.IsPresent())

Output:

Bool: false
Byte: false
Float32: false
Float64: false
Int16: false
Int32: false
Int64: false
Int: false
Rune: false
String: false
Time: false
Uint16: false
Uint32: false
Uint64: false
Uint64: false
Uint: false
Uintptr: false

Code:

s := struct {
    Bool    optional.Bool    `json:"bool"`
    Byte    optional.Byte    `json:"byte"`
    Float32 optional.Float32 `json:"float32"`
    Float64 optional.Float64 `json:"float64"`
    Int16   optional.Int16   `json:"int16"`
    Int32   optional.Int32   `json:"int32"`
    Int64   optional.Int64   `json:"int64"`
    Int     optional.Int     `json:"int"`
    Rune    optional.Rune    `json:"rune"`
    String  optional.String  `json:"string"`
    Time    optional.Time    `json:"time"`
    Uint16  optional.Uint16  `json:"uint16"`
    Uint32  optional.Uint32  `json:"uint32"`
    Uint64  optional.Uint64  `json:"uint64"`
    Uint    optional.Uint    `json:"uint"`
    Uintptr optional.Uintptr `json:"uintptr"`
}{}

x := `{
  "bool": false,
  "byte": 0,
  "float32": 0,
  "float64": 0,
  "int16": 0,
  "int32": 0,
  "int64": 0,
  "int": 0,
  "rune": 0,
  "string": "string",
  "time": "0001-01-01T00:00:00Z",
  "uint16": 0,
  "uint32": 0,
  "uint64": 0,
  "uint": 0,
  "uintptr": 0
}`
json.Unmarshal([]byte(x), &s)
fmt.Println("Bool:", s.Bool.IsPresent(), s.Bool)
fmt.Println("Byte:", s.Byte.IsPresent(), s.Byte)
fmt.Println("Float32:", s.Float32.IsPresent(), s.Float32)
fmt.Println("Float64:", s.Float64.IsPresent(), s.Float64)
fmt.Println("Int16:", s.Int16.IsPresent(), s.Int16)
fmt.Println("Int32:", s.Int32.IsPresent(), s.Int32)
fmt.Println("Int64:", s.Int64.IsPresent(), s.Int64)
fmt.Println("Int:", s.Int.IsPresent(), s.Int)
fmt.Println("Rune:", s.Rune.IsPresent(), s.Rune)
fmt.Println("String:", s.String.IsPresent(), s.String)
fmt.Println("Time:", s.Time.IsPresent(), s.Time)
fmt.Println("Uint16:", s.Uint16.IsPresent(), s.Uint16)
fmt.Println("Uint32:", s.Uint32.IsPresent(), s.Uint32)
fmt.Println("Uint64:", s.Uint64.IsPresent(), s.Uint64)
fmt.Println("Uint64:", s.Uint64.IsPresent(), s.Uint64)
fmt.Println("Uint:", s.Uint.IsPresent(), s.Uint)
fmt.Println("Uintptr:", s.Uint.IsPresent(), s.Uint)

Output:

Bool: true false
Byte: true 0
Float32: true 0
Float64: true 0
Int16: true 0
Int32: true 0
Int64: true 0
Int: true 0
Rune: true 0
String: true string
Time: true 0001-01-01 00:00:00 +0000 UTC
Uint16: true 0
Uint32: true 0
Uint64: true 0
Uint64: true 0
Uint: true 0
Uintptr: true 0

Code:

s := struct {
    XMLName xml.Name         `xml:"s"`
    Bool    optional.Bool    `xml:"bool"`
    Byte    optional.Byte    `xml:"byte"`
    Float32 optional.Float32 `xml:"float32"`
    Float64 optional.Float64 `xml:"float64"`
    Int16   optional.Int16   `xml:"int16"`
    Int32   optional.Int32   `xml:"int32"`
    Int64   optional.Int64   `xml:"int64"`
    Int     optional.Int     `xml:"int"`
    Rune    optional.Rune    `xml:"rune"`
    String  optional.String  `xml:"string"`
    Time    optional.Time    `xml:"time"`
    Uint16  optional.Uint16  `xml:"uint16"`
    Uint32  optional.Uint32  `xml:"uint32"`
    Uint64  optional.Uint64  `xml:"uint64"`
    Uint    optional.Uint    `xml:"uint"`
    Uintptr optional.Uintptr `xml:"uintptr"`
}{
    Bool:    optional.EmptyBool(),
    Byte:    optional.EmptyByte(),
    Float32: optional.EmptyFloat32(),
    Float64: optional.EmptyFloat64(),
    Int16:   optional.EmptyInt16(),
    Int32:   optional.EmptyInt32(),
    Int64:   optional.EmptyInt64(),
    Int:     optional.EmptyInt(),
    Rune:    optional.EmptyRune(),
    String:  optional.EmptyString(),
    Time:    optional.EmptyTime(),
    Uint16:  optional.EmptyUint16(),
    Uint32:  optional.EmptyUint32(),
    Uint64:  optional.EmptyUint64(),
    Uint:    optional.EmptyUint(),
    Uintptr: optional.EmptyUintptr(),
}

output, _ := xml.MarshalIndent(s, "", "  ")
fmt.Println(string(output))

Output:

<s>
  <bool>false</bool>
  <byte>0</byte>
  <float32>0</float32>
  <float64>0</float64>
  <int16>0</int16>
  <int32>0</int32>
  <int64>0</int64>
  <int>0</int>
  <rune>0</rune>
  <string></string>
  <time>0001-01-01T00:00:00Z</time>
  <uint16>0</uint16>
  <uint32>0</uint32>
  <uint64>0</uint64>
  <uint>0</uint>
  <uintptr>0</uintptr>
</s>

Code:

s := struct {
    XMLName xml.Name         `xml:"s"`
    Bool    optional.Bool    `xml:"bool,omitempty"`
    Byte    optional.Byte    `xml:"byte,omitempty"`
    Float32 optional.Float32 `xml:"float32,omitempty"`
    Float64 optional.Float64 `xml:"float64,omitempty"`
    Int16   optional.Int16   `xml:"int16,omitempty"`
    Int32   optional.Int32   `xml:"int32,omitempty"`
    Int64   optional.Int64   `xml:"int64,omitempty"`
    Int     optional.Int     `xml:"int,omitempty"`
    Rune    optional.Rune    `xml:"rune,omitempty"`
    String  optional.String  `xml:"string,omitempty"`
    Time    optional.Time    `xml:"time,omitempty"`
    Uint16  optional.Uint16  `xml:"uint16,omitempty"`
    Uint32  optional.Uint32  `xml:"uint32,omitempty"`
    Uint64  optional.Uint64  `xml:"uint64,omitempty"`
    Uint    optional.Uint    `xml:"uint,omitempty"`
    Uintptr optional.Uintptr `xml:"uintptr,omitempty"`
}{
    Bool:    optional.EmptyBool(),
    Byte:    optional.EmptyByte(),
    Float32: optional.EmptyFloat32(),
    Float64: optional.EmptyFloat64(),
    Int16:   optional.EmptyInt16(),
    Int32:   optional.EmptyInt32(),
    Int64:   optional.EmptyInt64(),
    Int:     optional.EmptyInt(),
    Rune:    optional.EmptyRune(),
    String:  optional.EmptyString(),
    Time:    optional.EmptyTime(),
    Uint16:  optional.EmptyUint16(),
    Uint32:  optional.EmptyUint32(),
    Uint64:  optional.EmptyUint64(),
    Uint:    optional.EmptyUint(),
    Uintptr: optional.EmptyUintptr(),
}

output, _ := xml.MarshalIndent(s, "", "  ")
fmt.Println(string(output))

Output:

<s></s>

Code:

s := struct {
    XMLName xml.Name         `xml:"s"`
    Bool    optional.Bool    `xml:"bool"`
    Byte    optional.Byte    `xml:"byte"`
    Float32 optional.Float32 `xml:"float32"`
    Float64 optional.Float64 `xml:"float64"`
    Int16   optional.Int16   `xml:"int16"`
    Int32   optional.Int32   `xml:"int32"`
    Int64   optional.Int64   `xml:"int64"`
    Int     optional.Int     `xml:"int"`
    Rune    optional.Rune    `xml:"rune"`
    String  optional.String  `xml:"string"`
    Time    optional.Time    `xml:"time"`
    Uint16  optional.Uint16  `xml:"uint16"`
    Uint32  optional.Uint32  `xml:"uint32"`
    Uint64  optional.Uint64  `xml:"uint64"`
    Uint    optional.Uint    `xml:"uint"`
    Uintptr optional.Uintptr `xml:"uintptr"`
}{
    Bool:    optional.OfBool(true),
    Byte:    optional.OfByte(1),
    Float32: optional.OfFloat32(2.1),
    Float64: optional.OfFloat64(2.2),
    Int16:   optional.OfInt16(3),
    Int32:   optional.OfInt32(4),
    Int64:   optional.OfInt64(5),
    Int:     optional.OfInt(6),
    Rune:    optional.OfRune(7),
    String:  optional.OfString("string"),
    Time:    optional.OfTime(time.Date(2006, 1, 2, 15, 4, 5, 0, time.UTC)),
    Uint16:  optional.OfUint16(8),
    Uint32:  optional.OfUint32(9),
    Uint64:  optional.OfUint64(10),
    Uint:    optional.OfUint(11),
    Uintptr: optional.OfUintptr(12),
}

output, _ := xml.MarshalIndent(s, "", "  ")
fmt.Println(string(output))

Output:

<s>
  <bool>true</bool>
  <byte>1</byte>
  <float32>2.1</float32>
  <float64>2.2</float64>
  <int16>3</int16>
  <int32>4</int32>
  <int64>5</int64>
  <int>6</int>
  <rune>7</rune>
  <string>string</string>
  <time>2006-01-02T15:04:05Z</time>
  <uint16>8</uint16>
  <uint32>9</uint32>
  <uint64>10</uint64>
  <uint>11</uint>
  <uintptr>12</uintptr>
</s>

Code:

s := struct {
    XMLName xml.Name         `xml:"s"`
    Bool    optional.Bool    `xml:"bool"`
    Byte    optional.Byte    `xml:"byte"`
    Float32 optional.Float32 `xml:"float32"`
    Float64 optional.Float64 `xml:"float64"`
    Int16   optional.Int16   `xml:"int16"`
    Int32   optional.Int32   `xml:"int32"`
    Int64   optional.Int64   `xml:"int64"`
    Int     optional.Int     `xml:"int"`
    Rune    optional.Rune    `xml:"rune"`
    String  optional.String  `xml:"string"`
    Time    optional.Time    `xml:"time"`
    Uint16  optional.Uint16  `xml:"uint16"`
    Uint32  optional.Uint32  `xml:"uint32"`
    Uint64  optional.Uint64  `xml:"uint64"`
    Uint    optional.Uint    `xml:"uint"`
    Uintptr optional.Uintptr `xml:"uintptr"`
}{}

x := `<s></s>`
xml.Unmarshal([]byte(x), &s)
fmt.Println("Bool:", s.Bool.IsPresent())
fmt.Println("Byte:", s.Byte.IsPresent())
fmt.Println("Float32:", s.Float32.IsPresent())
fmt.Println("Float64:", s.Float64.IsPresent())
fmt.Println("Int16:", s.Int16.IsPresent())
fmt.Println("Int32:", s.Int32.IsPresent())
fmt.Println("Int64:", s.Int64.IsPresent())
fmt.Println("Int:", s.Int.IsPresent())
fmt.Println("Rune:", s.Rune.IsPresent())
fmt.Println("String:", s.String.IsPresent())
fmt.Println("Time:", s.Time.IsPresent())
fmt.Println("Uint16:", s.Uint16.IsPresent())
fmt.Println("Uint32:", s.Uint32.IsPresent())
fmt.Println("Uint64:", s.Uint64.IsPresent())
fmt.Println("Uint64:", s.Uint64.IsPresent())
fmt.Println("Uint:", s.Uint.IsPresent())
fmt.Println("Uintptr:", s.Uint.IsPresent())

Output:

Bool: false
Byte: false
Float32: false
Float64: false
Int16: false
Int32: false
Int64: false
Int: false
Rune: false
String: false
Time: false
Uint16: false
Uint32: false
Uint64: false
Uint64: false
Uint: false
Uintptr: false

Code:

s := struct {
    XMLName xml.Name         `xml:"s"`
    Bool    optional.Bool    `xml:"bool"`
    Byte    optional.Byte    `xml:"byte"`
    Float32 optional.Float32 `xml:"float32"`
    Float64 optional.Float64 `xml:"float64"`
    Int16   optional.Int16   `xml:"int16"`
    Int32   optional.Int32   `xml:"int32"`
    Int64   optional.Int64   `xml:"int64"`
    Int     optional.Int     `xml:"int"`
    Rune    optional.Rune    `xml:"rune"`
    String  optional.String  `xml:"string"`
    Time    optional.Time    `xml:"time"`
    Uint16  optional.Uint16  `xml:"uint16"`
    Uint32  optional.Uint32  `xml:"uint32"`
    Uint64  optional.Uint64  `xml:"uint64"`
    Uint    optional.Uint    `xml:"uint"`
    Uintptr optional.Uintptr `xml:"uintptr"`
}{}

x := `<s>
  <bool>false</bool>
  <byte>0</byte>
  <float32>0</float32>
  <float64>0</float64>
  <int16>0</int16>
  <int32>0</int32>
  <int64>0</int64>
  <int>0</int>
  <rune>0</rune>
  <string>string</string>
  <time>0001-01-01T00:00:00Z</time>
  <uint16>0</uint16>
  <uint32>0</uint32>
  <uint64>0</uint64>
  <uint>0</uint>
  <uintptr>0</uintptr>
</s>`
xml.Unmarshal([]byte(x), &s)
fmt.Println("Bool:", s.Bool.IsPresent(), s.Bool)
fmt.Println("Byte:", s.Byte.IsPresent(), s.Byte)
fmt.Println("Float32:", s.Float32.IsPresent(), s.Float32)
fmt.Println("Float64:", s.Float64.IsPresent(), s.Float64)
fmt.Println("Int16:", s.Int16.IsPresent(), s.Int16)
fmt.Println("Int32:", s.Int32.IsPresent(), s.Int32)
fmt.Println("Int64:", s.Int64.IsPresent(), s.Int64)
fmt.Println("Int:", s.Int.IsPresent(), s.Int)
fmt.Println("Rune:", s.Rune.IsPresent(), s.Rune)
fmt.Println("String:", s.String.IsPresent(), s.String)
fmt.Println("Time:", s.Time.IsPresent(), s.Time)
fmt.Println("Uint16:", s.Uint16.IsPresent(), s.Uint16)
fmt.Println("Uint32:", s.Uint32.IsPresent(), s.Uint32)
fmt.Println("Uint64:", s.Uint64.IsPresent(), s.Uint64)
fmt.Println("Uint64:", s.Uint64.IsPresent(), s.Uint64)
fmt.Println("Uint:", s.Uint.IsPresent(), s.Uint)
fmt.Println("Uintptr:", s.Uint.IsPresent(), s.Uint)

Output:

Bool: true false
Byte: true 0
Float32: true 0
Float64: true 0
Int16: true 0
Int32: true 0
Int64: true 0
Int: true 0
Rune: true 0
String: true string
Time: true 0001-01-01 00:00:00 +0000 UTC
Uint16: true 0
Uint32: true 0
Uint64: true 0
Uint64: true 0
Uint: true 0
Uintptr: true 0

Index

Examples

Package Files

bool_generated.go byte_generated.go complex128_generated.go complex64_generated.go doc.go float32_generated.go float64_generated.go int16_generated.go int32_generated.go int64_generated.go int8_generated.go int_generated.go rune_generated.go string_generated.go time_generated.go types.go uint16_generated.go uint32_generated.go uint64_generated.go uint8_generated.go uint_generated.go uintptr_generated.go

type Bool Uses

type Bool optionalBool

Optional wraps a value that may or may not be nil. If a value is present, it may be unwrapped to expose the underlying value.

func EmptyBool Uses

func EmptyBool() Bool

Empty returns an empty optional.

func OfBool Uses

func OfBool(value bool) Bool

Of wraps the value in an optional.

func OfBoolPtr Uses

func OfBoolPtr(ptr *bool) Bool

func (Bool) Else Uses

func (o Bool) Else(elseValue bool) (value bool)

Else returns the value wrapped by this optional, or the value passed in if there is no value wrapped by this optional.

func (Bool) ElseFunc Uses

func (o Bool) ElseFunc(f func() bool) (value bool)

func (Bool) ElseZero Uses

func (o Bool) ElseZero() (value bool)

ElseZero returns the value wrapped by this optional, or the zero value of the type wrapped if there is no value wrapped by this optional.

func (Bool) Get Uses

func (o Bool) Get() (value bool, ok bool)

Get returns the value wrapped by this optional, and an ok signal for whether a value was wrapped.

func (Bool) If Uses

func (o Bool) If(f func(value bool))

If calls the function if there is a value wrapped by this optional.

func (Bool) IsPresent Uses

func (o Bool) IsPresent() bool

IsPresent returns true if there is a value wrapped by this optional.

func (Bool) MarshalJSON Uses

func (o Bool) MarshalJSON() (data []byte, err error)

MarshalJSON marshals the value being wrapped to JSON. If there is no vale being wrapped, the zero value of its type is marshaled.

func (Bool) MarshalXML Uses

func (o Bool) MarshalXML(e *xml.Encoder, start xml.StartElement) error

MarshalXML marshals the value being wrapped to XML. If there is no vale being wrapped, the zero value of its type is marshaled.

func (Bool) String Uses

func (o Bool) String() string

String returns the string representation of the wrapped value, or the string representation of the zero value of the type wrapped if there is no value wrapped by this optional.

func (*Bool) UnmarshalJSON Uses

func (o *Bool) UnmarshalJSON(data []byte) error

UnmarshalJSON unmarshals the JSON into a value wrapped by this optional.

func (*Bool) UnmarshalXML Uses

func (o *Bool) UnmarshalXML(d *xml.Decoder, start xml.StartElement) error

UnmarshalXML unmarshals the XML into a value wrapped by this optional.

type Byte Uses

type Byte optionalByte

Optional wraps a value that may or may not be nil. If a value is present, it may be unwrapped to expose the underlying value.

func EmptyByte Uses

func EmptyByte() Byte

Empty returns an empty optional.

func OfByte Uses

func OfByte(value byte) Byte

Of wraps the value in an optional.

func OfBytePtr Uses

func OfBytePtr(ptr *byte) Byte

func (Byte) Else Uses

func (o Byte) Else(elseValue byte) (value byte)

Else returns the value wrapped by this optional, or the value passed in if there is no value wrapped by this optional.

func (Byte) ElseFunc Uses

func (o Byte) ElseFunc(f func() byte) (value byte)

func (Byte) ElseZero Uses

func (o Byte) ElseZero() (value byte)

ElseZero returns the value wrapped by this optional, or the zero value of the type wrapped if there is no value wrapped by this optional.

func (Byte) Get Uses

func (o Byte) Get() (value byte, ok bool)

Get returns the value wrapped by this optional, and an ok signal for whether a value was wrapped.

func (Byte) If Uses

func (o Byte) If(f func(value byte))

If calls the function if there is a value wrapped by this optional.

func (Byte) IsPresent Uses

func (o Byte) IsPresent() bool

IsPresent returns true if there is a value wrapped by this optional.

func (Byte) MarshalJSON Uses

func (o Byte) MarshalJSON() (data []byte, err error)

MarshalJSON marshals the value being wrapped to JSON. If there is no vale being wrapped, the zero value of its type is marshaled.

func (Byte) MarshalXML Uses

func (o Byte) MarshalXML(e *xml.Encoder, start xml.StartElement) error

MarshalXML marshals the value being wrapped to XML. If there is no vale being wrapped, the zero value of its type is marshaled.

func (Byte) String Uses

func (o Byte) String() string

String returns the string representation of the wrapped value, or the string representation of the zero value of the type wrapped if there is no value wrapped by this optional.

func (*Byte) UnmarshalJSON Uses

func (o *Byte) UnmarshalJSON(data []byte) error

UnmarshalJSON unmarshals the JSON into a value wrapped by this optional.

func (*Byte) UnmarshalXML Uses

func (o *Byte) UnmarshalXML(d *xml.Decoder, start xml.StartElement) error

UnmarshalXML unmarshals the XML into a value wrapped by this optional.

type Complex128 Uses

type Complex128 optionalComplex128

Optional wraps a value that may or may not be nil. If a value is present, it may be unwrapped to expose the underlying value.

func EmptyComplex128 Uses

func EmptyComplex128() Complex128

Empty returns an empty optional.

func OfComplex128 Uses

func OfComplex128(value complex128) Complex128

Of wraps the value in an optional.

func OfComplex128Ptr Uses

func OfComplex128Ptr(ptr *complex128) Complex128

func (Complex128) Else Uses

func (o Complex128) Else(elseValue complex128) (value complex128)

Else returns the value wrapped by this optional, or the value passed in if there is no value wrapped by this optional.

func (Complex128) ElseFunc Uses

func (o Complex128) ElseFunc(f func() complex128) (value complex128)

func (Complex128) ElseZero Uses

func (o Complex128) ElseZero() (value complex128)

ElseZero returns the value wrapped by this optional, or the zero value of the type wrapped if there is no value wrapped by this optional.

func (Complex128) Get Uses

func (o Complex128) Get() (value complex128, ok bool)

Get returns the value wrapped by this optional, and an ok signal for whether a value was wrapped.

func (Complex128) If Uses

func (o Complex128) If(f func(value complex128))

If calls the function if there is a value wrapped by this optional.

func (Complex128) IsPresent Uses

func (o Complex128) IsPresent() bool

IsPresent returns true if there is a value wrapped by this optional.

func (Complex128) MarshalJSON Uses

func (o Complex128) MarshalJSON() (data []byte, err error)

MarshalJSON marshals the value being wrapped to JSON. If there is no vale being wrapped, the zero value of its type is marshaled.

func (Complex128) MarshalXML Uses

func (o Complex128) MarshalXML(e *xml.Encoder, start xml.StartElement) error

MarshalXML marshals the value being wrapped to XML. If there is no vale being wrapped, the zero value of its type is marshaled.

func (Complex128) String Uses

func (o Complex128) String() string

String returns the string representation of the wrapped value, or the string representation of the zero value of the type wrapped if there is no value wrapped by this optional.

func (*Complex128) UnmarshalJSON Uses

func (o *Complex128) UnmarshalJSON(data []byte) error

UnmarshalJSON unmarshals the JSON into a value wrapped by this optional.

func (*Complex128) UnmarshalXML Uses

func (o *Complex128) UnmarshalXML(d *xml.Decoder, start xml.StartElement) error

UnmarshalXML unmarshals the XML into a value wrapped by this optional.

type Complex64 Uses

type Complex64 optionalComplex64

Optional wraps a value that may or may not be nil. If a value is present, it may be unwrapped to expose the underlying value.

func EmptyComplex64 Uses

func EmptyComplex64() Complex64

Empty returns an empty optional.

func OfComplex64 Uses

func OfComplex64(value complex64) Complex64

Of wraps the value in an optional.

func OfComplex64Ptr Uses

func OfComplex64Ptr(ptr *complex64) Complex64

func (Complex64) Else Uses

func (o Complex64) Else(elseValue complex64) (value complex64)

Else returns the value wrapped by this optional, or the value passed in if there is no value wrapped by this optional.

func (Complex64) ElseFunc Uses

func (o Complex64) ElseFunc(f func() complex64) (value complex64)

func (Complex64) ElseZero Uses

func (o Complex64) ElseZero() (value complex64)

ElseZero returns the value wrapped by this optional, or the zero value of the type wrapped if there is no value wrapped by this optional.

func (Complex64) Get Uses

func (o Complex64) Get() (value complex64, ok bool)

Get returns the value wrapped by this optional, and an ok signal for whether a value was wrapped.

func (Complex64) If Uses

func (o Complex64) If(f func(value complex64))

If calls the function if there is a value wrapped by this optional.

func (Complex64) IsPresent Uses

func (o Complex64) IsPresent() bool

IsPresent returns true if there is a value wrapped by this optional.

func (Complex64) MarshalJSON Uses

func (o Complex64) MarshalJSON() (data []byte, err error)

MarshalJSON marshals the value being wrapped to JSON. If there is no vale being wrapped, the zero value of its type is marshaled.

func (Complex64) MarshalXML Uses

func (o Complex64) MarshalXML(e *xml.Encoder, start xml.StartElement) error

MarshalXML marshals the value being wrapped to XML. If there is no vale being wrapped, the zero value of its type is marshaled.

func (Complex64) String Uses

func (o Complex64) String() string

String returns the string representation of the wrapped value, or the string representation of the zero value of the type wrapped if there is no value wrapped by this optional.

func (*Complex64) UnmarshalJSON Uses

func (o *Complex64) UnmarshalJSON(data []byte) error

UnmarshalJSON unmarshals the JSON into a value wrapped by this optional.

func (*Complex64) UnmarshalXML Uses

func (o *Complex64) UnmarshalXML(d *xml.Decoder, start xml.StartElement) error

UnmarshalXML unmarshals the XML into a value wrapped by this optional.

type Float32 Uses

type Float32 optionalFloat32

Optional wraps a value that may or may not be nil. If a value is present, it may be unwrapped to expose the underlying value.

func EmptyFloat32 Uses

func EmptyFloat32() Float32

Empty returns an empty optional.

func OfFloat32 Uses

func OfFloat32(value float32) Float32

Of wraps the value in an optional.

func OfFloat32Ptr Uses

func OfFloat32Ptr(ptr *float32) Float32

func (Float32) Else Uses

func (o Float32) Else(elseValue float32) (value float32)

Else returns the value wrapped by this optional, or the value passed in if there is no value wrapped by this optional.

func (Float32) ElseFunc Uses

func (o Float32) ElseFunc(f func() float32) (value float32)

func (Float32) ElseZero Uses

func (o Float32) ElseZero() (value float32)

ElseZero returns the value wrapped by this optional, or the zero value of the type wrapped if there is no value wrapped by this optional.

func (Float32) Get Uses

func (o Float32) Get() (value float32, ok bool)

Get returns the value wrapped by this optional, and an ok signal for whether a value was wrapped.

func (Float32) If Uses

func (o Float32) If(f func(value float32))

If calls the function if there is a value wrapped by this optional.

func (Float32) IsPresent Uses

func (o Float32) IsPresent() bool

IsPresent returns true if there is a value wrapped by this optional.

func (Float32) MarshalJSON Uses

func (o Float32) MarshalJSON() (data []byte, err error)

MarshalJSON marshals the value being wrapped to JSON. If there is no vale being wrapped, the zero value of its type is marshaled.

func (Float32) MarshalXML Uses

func (o Float32) MarshalXML(e *xml.Encoder, start xml.StartElement) error

MarshalXML marshals the value being wrapped to XML. If there is no vale being wrapped, the zero value of its type is marshaled.

func (Float32) String Uses

func (o Float32) String() string

String returns the string representation of the wrapped value, or the string representation of the zero value of the type wrapped if there is no value wrapped by this optional.

func (*Float32) UnmarshalJSON Uses

func (o *Float32) UnmarshalJSON(data []byte) error

UnmarshalJSON unmarshals the JSON into a value wrapped by this optional.

func (*Float32) UnmarshalXML Uses

func (o *Float32) UnmarshalXML(d *xml.Decoder, start xml.StartElement) error

UnmarshalXML unmarshals the XML into a value wrapped by this optional.

type Float64 Uses

type Float64 optionalFloat64

Optional wraps a value that may or may not be nil. If a value is present, it may be unwrapped to expose the underlying value.

func EmptyFloat64 Uses

func EmptyFloat64() Float64

Empty returns an empty optional.

func OfFloat64 Uses

func OfFloat64(value float64) Float64

Of wraps the value in an optional.

func OfFloat64Ptr Uses

func OfFloat64Ptr(ptr *float64) Float64

func (Float64) Else Uses

func (o Float64) Else(elseValue float64) (value float64)

Else returns the value wrapped by this optional, or the value passed in if there is no value wrapped by this optional.

func (Float64) ElseFunc Uses

func (o Float64) ElseFunc(f func() float64) (value float64)

func (Float64) ElseZero Uses

func (o Float64) ElseZero() (value float64)

ElseZero returns the value wrapped by this optional, or the zero value of the type wrapped if there is no value wrapped by this optional.

func (Float64) Get Uses

func (o Float64) Get() (value float64, ok bool)

Get returns the value wrapped by this optional, and an ok signal for whether a value was wrapped.

func (Float64) If Uses

func (o Float64) If(f func(value float64))

If calls the function if there is a value wrapped by this optional.

func (Float64) IsPresent Uses

func (o Float64) IsPresent() bool

IsPresent returns true if there is a value wrapped by this optional.

func (Float64) MarshalJSON Uses

func (o Float64) MarshalJSON() (data []byte, err error)

MarshalJSON marshals the value being wrapped to JSON. If there is no vale being wrapped, the zero value of its type is marshaled.

func (Float64) MarshalXML Uses

func (o Float64) MarshalXML(e *xml.Encoder, start xml.StartElement) error

MarshalXML marshals the value being wrapped to XML. If there is no vale being wrapped, the zero value of its type is marshaled.

func (Float64) String Uses

func (o Float64) String() string

String returns the string representation of the wrapped value, or the string representation of the zero value of the type wrapped if there is no value wrapped by this optional.

func (*Float64) UnmarshalJSON Uses

func (o *Float64) UnmarshalJSON(data []byte) error

UnmarshalJSON unmarshals the JSON into a value wrapped by this optional.

func (*Float64) UnmarshalXML Uses

func (o *Float64) UnmarshalXML(d *xml.Decoder, start xml.StartElement) error

UnmarshalXML unmarshals the XML into a value wrapped by this optional.

type Int Uses

type Int optionalInt

Optional wraps a value that may or may not be nil. If a value is present, it may be unwrapped to expose the underlying value.

func EmptyInt Uses

func EmptyInt() Int

Empty returns an empty optional.

func OfInt Uses

func OfInt(value int) Int

Of wraps the value in an optional.

func OfIntPtr Uses

func OfIntPtr(ptr *int) Int

func (Int) Else Uses

func (o Int) Else(elseValue int) (value int)

Else returns the value wrapped by this optional, or the value passed in if there is no value wrapped by this optional.

func (Int) ElseFunc Uses

func (o Int) ElseFunc(f func() int) (value int)

func (Int) ElseZero Uses

func (o Int) ElseZero() (value int)

ElseZero returns the value wrapped by this optional, or the zero value of the type wrapped if there is no value wrapped by this optional.

func (Int) Get Uses

func (o Int) Get() (value int, ok bool)

Get returns the value wrapped by this optional, and an ok signal for whether a value was wrapped.

func (Int) If Uses

func (o Int) If(f func(value int))

If calls the function if there is a value wrapped by this optional.

func (Int) IsPresent Uses

func (o Int) IsPresent() bool

IsPresent returns true if there is a value wrapped by this optional.

func (Int) MarshalJSON Uses

func (o Int) MarshalJSON() (data []byte, err error)

MarshalJSON marshals the value being wrapped to JSON. If there is no vale being wrapped, the zero value of its type is marshaled.

func (Int) MarshalXML Uses

func (o Int) MarshalXML(e *xml.Encoder, start xml.StartElement) error

MarshalXML marshals the value being wrapped to XML. If there is no vale being wrapped, the zero value of its type is marshaled.

func (Int) String Uses

func (o Int) String() string

String returns the string representation of the wrapped value, or the string representation of the zero value of the type wrapped if there is no value wrapped by this optional.

func (*Int) UnmarshalJSON Uses

func (o *Int) UnmarshalJSON(data []byte) error

UnmarshalJSON unmarshals the JSON into a value wrapped by this optional.

func (*Int) UnmarshalXML Uses

func (o *Int) UnmarshalXML(d *xml.Decoder, start xml.StartElement) error

UnmarshalXML unmarshals the XML into a value wrapped by this optional.

type Int16 Uses

type Int16 optionalInt16

Optional wraps a value that may or may not be nil. If a value is present, it may be unwrapped to expose the underlying value.

func EmptyInt16 Uses

func EmptyInt16() Int16

Empty returns an empty optional.

func OfInt16 Uses

func OfInt16(value int16) Int16

Of wraps the value in an optional.

func OfInt16Ptr Uses

func OfInt16Ptr(ptr *int16) Int16

func (Int16) Else Uses

func (o Int16) Else(elseValue int16) (value int16)

Else returns the value wrapped by this optional, or the value passed in if there is no value wrapped by this optional.

func (Int16) ElseFunc Uses

func (o Int16) ElseFunc(f func() int16) (value int16)

func (Int16) ElseZero Uses

func (o Int16) ElseZero() (value int16)

ElseZero returns the value wrapped by this optional, or the zero value of the type wrapped if there is no value wrapped by this optional.

func (Int16) Get Uses

func (o Int16) Get() (value int16, ok bool)

Get returns the value wrapped by this optional, and an ok signal for whether a value was wrapped.

func (Int16) If Uses

func (o Int16) If(f func(value int16))

If calls the function if there is a value wrapped by this optional.

func (Int16) IsPresent Uses

func (o Int16) IsPresent() bool

IsPresent returns true if there is a value wrapped by this optional.

func (Int16) MarshalJSON Uses

func (o Int16) MarshalJSON() (data []byte, err error)

MarshalJSON marshals the value being wrapped to JSON. If there is no vale being wrapped, the zero value of its type is marshaled.

func (Int16) MarshalXML Uses

func (o Int16) MarshalXML(e *xml.Encoder, start xml.StartElement) error

MarshalXML marshals the value being wrapped to XML. If there is no vale being wrapped, the zero value of its type is marshaled.

func (Int16) String Uses

func (o Int16) String() string

String returns the string representation of the wrapped value, or the string representation of the zero value of the type wrapped if there is no value wrapped by this optional.

func (*Int16) UnmarshalJSON Uses

func (o *Int16) UnmarshalJSON(data []byte) error

UnmarshalJSON unmarshals the JSON into a value wrapped by this optional.

func (*Int16) UnmarshalXML Uses

func (o *Int16) UnmarshalXML(d *xml.Decoder, start xml.StartElement) error

UnmarshalXML unmarshals the XML into a value wrapped by this optional.

type Int32 Uses

type Int32 optionalInt32

Optional wraps a value that may or may not be nil. If a value is present, it may be unwrapped to expose the underlying value.

func EmptyInt32 Uses

func EmptyInt32() Int32

Empty returns an empty optional.

func OfInt32 Uses

func OfInt32(value int32) Int32

Of wraps the value in an optional.

func OfInt32Ptr Uses

func OfInt32Ptr(ptr *int32) Int32

func (Int32) Else Uses

func (o Int32) Else(elseValue int32) (value int32)

Else returns the value wrapped by this optional, or the value passed in if there is no value wrapped by this optional.

func (Int32) ElseFunc Uses

func (o Int32) ElseFunc(f func() int32) (value int32)

func (Int32) ElseZero Uses

func (o Int32) ElseZero() (value int32)

ElseZero returns the value wrapped by this optional, or the zero value of the type wrapped if there is no value wrapped by this optional.

func (Int32) Get Uses

func (o Int32) Get() (value int32, ok bool)

Get returns the value wrapped by this optional, and an ok signal for whether a value was wrapped.

func (Int32) If Uses

func (o Int32) If(f func(value int32))

If calls the function if there is a value wrapped by this optional.

func (Int32) IsPresent Uses

func (o Int32) IsPresent() bool

IsPresent returns true if there is a value wrapped by this optional.

func (Int32) MarshalJSON Uses

func (o Int32) MarshalJSON() (data []byte, err error)

MarshalJSON marshals the value being wrapped to JSON. If there is no vale being wrapped, the zero value of its type is marshaled.

func (Int32) MarshalXML Uses

func (o Int32) MarshalXML(e *xml.Encoder, start xml.StartElement) error

MarshalXML marshals the value being wrapped to XML. If there is no vale being wrapped, the zero value of its type is marshaled.

func (Int32) String Uses

func (o Int32) String() string

String returns the string representation of the wrapped value, or the string representation of the zero value of the type wrapped if there is no value wrapped by this optional.

func (*Int32) UnmarshalJSON Uses

func (o *Int32) UnmarshalJSON(data []byte) error

UnmarshalJSON unmarshals the JSON into a value wrapped by this optional.

func (*Int32) UnmarshalXML Uses

func (o *Int32) UnmarshalXML(d *xml.Decoder, start xml.StartElement) error

UnmarshalXML unmarshals the XML into a value wrapped by this optional.

type Int64 Uses

type Int64 optionalInt64

Optional wraps a value that may or may not be nil. If a value is present, it may be unwrapped to expose the underlying value.

func EmptyInt64 Uses

func EmptyInt64() Int64

Empty returns an empty optional.

func OfInt64 Uses

func OfInt64(value int64) Int64

Of wraps the value in an optional.

func OfInt64Ptr Uses

func OfInt64Ptr(ptr *int64) Int64

func (Int64) Else Uses

func (o Int64) Else(elseValue int64) (value int64)

Else returns the value wrapped by this optional, or the value passed in if there is no value wrapped by this optional.

func (Int64) ElseFunc Uses

func (o Int64) ElseFunc(f func() int64) (value int64)

func (Int64) ElseZero Uses

func (o Int64) ElseZero() (value int64)

ElseZero returns the value wrapped by this optional, or the zero value of the type wrapped if there is no value wrapped by this optional.

func (Int64) Get Uses

func (o Int64) Get() (value int64, ok bool)

Get returns the value wrapped by this optional, and an ok signal for whether a value was wrapped.

func (Int64) If Uses

func (o Int64) If(f func(value int64))

If calls the function if there is a value wrapped by this optional.

func (Int64) IsPresent Uses

func (o Int64) IsPresent() bool

IsPresent returns true if there is a value wrapped by this optional.

func (Int64) MarshalJSON Uses

func (o Int64) MarshalJSON() (data []byte, err error)

MarshalJSON marshals the value being wrapped to JSON. If there is no vale being wrapped, the zero value of its type is marshaled.

func (Int64) MarshalXML Uses

func (o Int64) MarshalXML(e *xml.Encoder, start xml.StartElement) error

MarshalXML marshals the value being wrapped to XML. If there is no vale being wrapped, the zero value of its type is marshaled.

func (Int64) String Uses

func (o Int64) String() string

String returns the string representation of the wrapped value, or the string representation of the zero value of the type wrapped if there is no value wrapped by this optional.

func (*Int64) UnmarshalJSON Uses

func (o *Int64) UnmarshalJSON(data []byte) error

UnmarshalJSON unmarshals the JSON into a value wrapped by this optional.

func (*Int64) UnmarshalXML Uses

func (o *Int64) UnmarshalXML(d *xml.Decoder, start xml.StartElement) error

UnmarshalXML unmarshals the XML into a value wrapped by this optional.

type Int8 Uses

type Int8 optionalInt8

Optional wraps a value that may or may not be nil. If a value is present, it may be unwrapped to expose the underlying value.

func EmptyInt8 Uses

func EmptyInt8() Int8

Empty returns an empty optional.

func OfInt8 Uses

func OfInt8(value int8) Int8

Of wraps the value in an optional.

func OfInt8Ptr Uses

func OfInt8Ptr(ptr *int8) Int8

func (Int8) Else Uses

func (o Int8) Else(elseValue int8) (value int8)

Else returns the value wrapped by this optional, or the value passed in if there is no value wrapped by this optional.

func (Int8) ElseFunc Uses

func (o Int8) ElseFunc(f func() int8) (value int8)

func (Int8) ElseZero Uses

func (o Int8) ElseZero() (value int8)

ElseZero returns the value wrapped by this optional, or the zero value of the type wrapped if there is no value wrapped by this optional.

func (Int8) Get Uses

func (o Int8) Get() (value int8, ok bool)

Get returns the value wrapped by this optional, and an ok signal for whether a value was wrapped.

func (Int8) If Uses

func (o Int8) If(f func(value int8))

If calls the function if there is a value wrapped by this optional.

func (Int8) IsPresent Uses

func (o Int8) IsPresent() bool

IsPresent returns true if there is a value wrapped by this optional.

func (Int8) MarshalJSON Uses

func (o Int8) MarshalJSON() (data []byte, err error)

MarshalJSON marshals the value being wrapped to JSON. If there is no vale being wrapped, the zero value of its type is marshaled.

func (Int8) MarshalXML Uses

func (o Int8) MarshalXML(e *xml.Encoder, start xml.StartElement) error

MarshalXML marshals the value being wrapped to XML. If there is no vale being wrapped, the zero value of its type is marshaled.

func (Int8) String Uses

func (o Int8) String() string

String returns the string representation of the wrapped value, or the string representation of the zero value of the type wrapped if there is no value wrapped by this optional.

func (*Int8) UnmarshalJSON Uses

func (o *Int8) UnmarshalJSON(data []byte) error

UnmarshalJSON unmarshals the JSON into a value wrapped by this optional.

func (*Int8) UnmarshalXML Uses

func (o *Int8) UnmarshalXML(d *xml.Decoder, start xml.StartElement) error

UnmarshalXML unmarshals the XML into a value wrapped by this optional.

type Rune Uses

type Rune optionalRune

Optional wraps a value that may or may not be nil. If a value is present, it may be unwrapped to expose the underlying value.

func EmptyRune Uses

func EmptyRune() Rune

Empty returns an empty optional.

func OfRune Uses

func OfRune(value rune) Rune

Of wraps the value in an optional.

func OfRunePtr Uses

func OfRunePtr(ptr *rune) Rune

func (Rune) Else Uses

func (o Rune) Else(elseValue rune) (value rune)

Else returns the value wrapped by this optional, or the value passed in if there is no value wrapped by this optional.

func (Rune) ElseFunc Uses

func (o Rune) ElseFunc(f func() rune) (value rune)

func (Rune) ElseZero Uses

func (o Rune) ElseZero() (value rune)

ElseZero returns the value wrapped by this optional, or the zero value of the type wrapped if there is no value wrapped by this optional.

func (Rune) Get Uses

func (o Rune) Get() (value rune, ok bool)

Get returns the value wrapped by this optional, and an ok signal for whether a value was wrapped.

func (Rune) If Uses

func (o Rune) If(f func(value rune))

If calls the function if there is a value wrapped by this optional.

func (Rune) IsPresent Uses

func (o Rune) IsPresent() bool

IsPresent returns true if there is a value wrapped by this optional.

func (Rune) MarshalJSON Uses

func (o Rune) MarshalJSON() (data []byte, err error)

MarshalJSON marshals the value being wrapped to JSON. If there is no vale being wrapped, the zero value of its type is marshaled.

func (Rune) MarshalXML Uses

func (o Rune) MarshalXML(e *xml.Encoder, start xml.StartElement) error

MarshalXML marshals the value being wrapped to XML. If there is no vale being wrapped, the zero value of its type is marshaled.

func (Rune) String Uses

func (o Rune) String() string

String returns the string representation of the wrapped value, or the string representation of the zero value of the type wrapped if there is no value wrapped by this optional.

func (*Rune) UnmarshalJSON Uses

func (o *Rune) UnmarshalJSON(data []byte) error

UnmarshalJSON unmarshals the JSON into a value wrapped by this optional.

func (*Rune) UnmarshalXML Uses

func (o *Rune) UnmarshalXML(d *xml.Decoder, start xml.StartElement) error

UnmarshalXML unmarshals the XML into a value wrapped by this optional.

type String Uses

type String optionalString

Optional wraps a value that may or may not be nil. If a value is present, it may be unwrapped to expose the underlying value.

func EmptyString Uses

func EmptyString() String

Empty returns an empty optional.

func OfString Uses

func OfString(value string) String

Of wraps the value in an optional.

func OfStringPtr Uses

func OfStringPtr(ptr *string) String

func (String) Else Uses

func (o String) Else(elseValue string) (value string)

Else returns the value wrapped by this optional, or the value passed in if there is no value wrapped by this optional.

func (String) ElseFunc Uses

func (o String) ElseFunc(f func() string) (value string)

func (String) ElseZero Uses

func (o String) ElseZero() (value string)

ElseZero returns the value wrapped by this optional, or the zero value of the type wrapped if there is no value wrapped by this optional.

func (String) Get Uses

func (o String) Get() (value string, ok bool)

Get returns the value wrapped by this optional, and an ok signal for whether a value was wrapped.

func (String) If Uses

func (o String) If(f func(value string))

If calls the function if there is a value wrapped by this optional.

func (String) IsPresent Uses

func (o String) IsPresent() bool

IsPresent returns true if there is a value wrapped by this optional.

func (String) MarshalJSON Uses

func (o String) MarshalJSON() (data []byte, err error)

MarshalJSON marshals the value being wrapped to JSON. If there is no vale being wrapped, the zero value of its type is marshaled.

func (String) MarshalXML Uses

func (o String) MarshalXML(e *xml.Encoder, start xml.StartElement) error

MarshalXML marshals the value being wrapped to XML. If there is no vale being wrapped, the zero value of its type is marshaled.

func (String) String Uses

func (o String) String() string

String returns the string representation of the wrapped value, or the string representation of the zero value of the type wrapped if there is no value wrapped by this optional.

func (*String) UnmarshalJSON Uses

func (o *String) UnmarshalJSON(data []byte) error

UnmarshalJSON unmarshals the JSON into a value wrapped by this optional.

func (*String) UnmarshalXML Uses

func (o *String) UnmarshalXML(d *xml.Decoder, start xml.StartElement) error

UnmarshalXML unmarshals the XML into a value wrapped by this optional.

type Time Uses

type Time optionalTime

Optional wraps a value that may or may not be nil. If a value is present, it may be unwrapped to expose the underlying value.

func EmptyTime Uses

func EmptyTime() Time

Empty returns an empty optional.

func OfTime Uses

func OfTime(value time.Time) Time

Of wraps the value in an optional.

func OfTimePtr Uses

func OfTimePtr(ptr *time.Time) Time

func (Time) Else Uses

func (o Time) Else(elseValue time.Time) (value time.Time)

Else returns the value wrapped by this optional, or the value passed in if there is no value wrapped by this optional.

func (Time) ElseFunc Uses

func (o Time) ElseFunc(f func() time.Time) (value time.Time)

func (Time) ElseZero Uses

func (o Time) ElseZero() (value time.Time)

ElseZero returns the value wrapped by this optional, or the zero value of the type wrapped if there is no value wrapped by this optional.

func (Time) Get Uses

func (o Time) Get() (value time.Time, ok bool)

Get returns the value wrapped by this optional, and an ok signal for whether a value was wrapped.

func (Time) If Uses

func (o Time) If(f func(value time.Time))

If calls the function if there is a value wrapped by this optional.

func (Time) IsPresent Uses

func (o Time) IsPresent() bool

IsPresent returns true if there is a value wrapped by this optional.

func (Time) MarshalJSON Uses

func (o Time) MarshalJSON() (data []byte, err error)

MarshalJSON marshals the value being wrapped to JSON. If there is no vale being wrapped, the zero value of its type is marshaled.

func (Time) MarshalXML Uses

func (o Time) MarshalXML(e *xml.Encoder, start xml.StartElement) error

MarshalXML marshals the value being wrapped to XML. If there is no vale being wrapped, the zero value of its type is marshaled.

func (Time) String Uses

func (o Time) String() string

String returns the string representation of the wrapped value, or the string representation of the zero value of the type wrapped if there is no value wrapped by this optional.

func (*Time) UnmarshalJSON Uses

func (o *Time) UnmarshalJSON(data []byte) error

UnmarshalJSON unmarshals the JSON into a value wrapped by this optional.

func (*Time) UnmarshalXML Uses

func (o *Time) UnmarshalXML(d *xml.Decoder, start xml.StartElement) error

UnmarshalXML unmarshals the XML into a value wrapped by this optional.

type Uint Uses

type Uint optionalUint

Optional wraps a value that may or may not be nil. If a value is present, it may be unwrapped to expose the underlying value.

func EmptyUint Uses

func EmptyUint() Uint

Empty returns an empty optional.

func OfUint Uses

func OfUint(value uint) Uint

Of wraps the value in an optional.

func OfUintPtr Uses

func OfUintPtr(ptr *uint) Uint

func (Uint) Else Uses

func (o Uint) Else(elseValue uint) (value uint)

Else returns the value wrapped by this optional, or the value passed in if there is no value wrapped by this optional.

func (Uint) ElseFunc Uses

func (o Uint) ElseFunc(f func() uint) (value uint)

func (Uint) ElseZero Uses

func (o Uint) ElseZero() (value uint)

ElseZero returns the value wrapped by this optional, or the zero value of the type wrapped if there is no value wrapped by this optional.

func (Uint) Get Uses

func (o Uint) Get() (value uint, ok bool)

Get returns the value wrapped by this optional, and an ok signal for whether a value was wrapped.

func (Uint) If Uses

func (o Uint) If(f func(value uint))

If calls the function if there is a value wrapped by this optional.

func (Uint) IsPresent Uses

func (o Uint) IsPresent() bool

IsPresent returns true if there is a value wrapped by this optional.

func (Uint) MarshalJSON Uses

func (o Uint) MarshalJSON() (data []byte, err error)

MarshalJSON marshals the value being wrapped to JSON. If there is no vale being wrapped, the zero value of its type is marshaled.

func (Uint) MarshalXML Uses

func (o Uint) MarshalXML(e *xml.Encoder, start xml.StartElement) error

MarshalXML marshals the value being wrapped to XML. If there is no vale being wrapped, the zero value of its type is marshaled.

func (Uint) String Uses

func (o Uint) String() string

String returns the string representation of the wrapped value, or the string representation of the zero value of the type wrapped if there is no value wrapped by this optional.

func (*Uint) UnmarshalJSON Uses

func (o *Uint) UnmarshalJSON(data []byte) error

UnmarshalJSON unmarshals the JSON into a value wrapped by this optional.

func (*Uint) UnmarshalXML Uses

func (o *Uint) UnmarshalXML(d *xml.Decoder, start xml.StartElement) error

UnmarshalXML unmarshals the XML into a value wrapped by this optional.

type Uint16 Uses

type Uint16 optionalUint16

Optional wraps a value that may or may not be nil. If a value is present, it may be unwrapped to expose the underlying value.

func EmptyUint16 Uses

func EmptyUint16() Uint16

Empty returns an empty optional.

func OfUint16 Uses

func OfUint16(value uint16) Uint16

Of wraps the value in an optional.

func OfUint16Ptr Uses

func OfUint16Ptr(ptr *uint16) Uint16

func (Uint16) Else Uses

func (o Uint16) Else(elseValue uint16) (value uint16)

Else returns the value wrapped by this optional, or the value passed in if there is no value wrapped by this optional.

func (Uint16) ElseFunc Uses

func (o Uint16) ElseFunc(f func() uint16) (value uint16)

func (Uint16) ElseZero Uses

func (o Uint16) ElseZero() (value uint16)

ElseZero returns the value wrapped by this optional, or the zero value of the type wrapped if there is no value wrapped by this optional.

func (Uint16) Get Uses

func (o Uint16) Get() (value uint16, ok bool)

Get returns the value wrapped by this optional, and an ok signal for whether a value was wrapped.

func (Uint16) If Uses

func (o Uint16) If(f func(value uint16))

If calls the function if there is a value wrapped by this optional.

func (Uint16) IsPresent Uses

func (o Uint16) IsPresent() bool

IsPresent returns true if there is a value wrapped by this optional.

func (Uint16) MarshalJSON Uses

func (o Uint16) MarshalJSON() (data []byte, err error)

MarshalJSON marshals the value being wrapped to JSON. If there is no vale being wrapped, the zero value of its type is marshaled.

func (Uint16) MarshalXML Uses

func (o Uint16) MarshalXML(e *xml.Encoder, start xml.StartElement) error

MarshalXML marshals the value being wrapped to XML. If there is no vale being wrapped, the zero value of its type is marshaled.

func (Uint16) String Uses

func (o Uint16) String() string

String returns the string representation of the wrapped value, or the string representation of the zero value of the type wrapped if there is no value wrapped by this optional.

func (*Uint16) UnmarshalJSON Uses

func (o *Uint16) UnmarshalJSON(data []byte) error

UnmarshalJSON unmarshals the JSON into a value wrapped by this optional.

func (*Uint16) UnmarshalXML Uses

func (o *Uint16) UnmarshalXML(d *xml.Decoder, start xml.StartElement) error

UnmarshalXML unmarshals the XML into a value wrapped by this optional.

type Uint32 Uses

type Uint32 optionalUint32

Optional wraps a value that may or may not be nil. If a value is present, it may be unwrapped to expose the underlying value.

func EmptyUint32 Uses

func EmptyUint32() Uint32

Empty returns an empty optional.

func OfUint32 Uses

func OfUint32(value uint32) Uint32

Of wraps the value in an optional.

func OfUint32Ptr Uses

func OfUint32Ptr(ptr *uint32) Uint32

func (Uint32) Else Uses

func (o Uint32) Else(elseValue uint32) (value uint32)

Else returns the value wrapped by this optional, or the value passed in if there is no value wrapped by this optional.

func (Uint32) ElseFunc Uses

func (o Uint32) ElseFunc(f func() uint32) (value uint32)

func (Uint32) ElseZero Uses

func (o Uint32) ElseZero() (value uint32)

ElseZero returns the value wrapped by this optional, or the zero value of the type wrapped if there is no value wrapped by this optional.

func (Uint32) Get Uses

func (o Uint32) Get() (value uint32, ok bool)

Get returns the value wrapped by this optional, and an ok signal for whether a value was wrapped.

func (Uint32) If Uses

func (o Uint32) If(f func(value uint32))

If calls the function if there is a value wrapped by this optional.

func (Uint32) IsPresent Uses

func (o Uint32) IsPresent() bool

IsPresent returns true if there is a value wrapped by this optional.

func (Uint32) MarshalJSON Uses

func (o Uint32) MarshalJSON() (data []byte, err error)

MarshalJSON marshals the value being wrapped to JSON. If there is no vale being wrapped, the zero value of its type is marshaled.

func (Uint32) MarshalXML Uses

func (o Uint32) MarshalXML(e *xml.Encoder, start xml.StartElement) error

MarshalXML marshals the value being wrapped to XML. If there is no vale being wrapped, the zero value of its type is marshaled.

func (Uint32) String Uses

func (o Uint32) String() string

String returns the string representation of the wrapped value, or the string representation of the zero value of the type wrapped if there is no value wrapped by this optional.

func (*Uint32) UnmarshalJSON Uses

func (o *Uint32) UnmarshalJSON(data []byte) error

UnmarshalJSON unmarshals the JSON into a value wrapped by this optional.

func (*Uint32) UnmarshalXML Uses

func (o *Uint32) UnmarshalXML(d *xml.Decoder, start xml.StartElement) error

UnmarshalXML unmarshals the XML into a value wrapped by this optional.

type Uint64 Uses

type Uint64 optionalUint64

Optional wraps a value that may or may not be nil. If a value is present, it may be unwrapped to expose the underlying value.

func EmptyUint64 Uses

func EmptyUint64() Uint64

Empty returns an empty optional.

func OfUint64 Uses

func OfUint64(value uint64) Uint64

Of wraps the value in an optional.

func OfUint64Ptr Uses

func OfUint64Ptr(ptr *uint64) Uint64

func (Uint64) Else Uses

func (o Uint64) Else(elseValue uint64) (value uint64)

Else returns the value wrapped by this optional, or the value passed in if there is no value wrapped by this optional.

func (Uint64) ElseFunc Uses

func (o Uint64) ElseFunc(f func() uint64) (value uint64)

func (Uint64) ElseZero Uses

func (o Uint64) ElseZero() (value uint64)

ElseZero returns the value wrapped by this optional, or the zero value of the type wrapped if there is no value wrapped by this optional.

func (Uint64) Get Uses

func (o Uint64) Get() (value uint64, ok bool)

Get returns the value wrapped by this optional, and an ok signal for whether a value was wrapped.

func (Uint64) If Uses

func (o Uint64) If(f func(value uint64))

If calls the function if there is a value wrapped by this optional.

func (Uint64) IsPresent Uses

func (o Uint64) IsPresent() bool

IsPresent returns true if there is a value wrapped by this optional.

func (Uint64) MarshalJSON Uses

func (o Uint64) MarshalJSON() (data []byte, err error)

MarshalJSON marshals the value being wrapped to JSON. If there is no vale being wrapped, the zero value of its type is marshaled.

func (Uint64) MarshalXML Uses

func (o Uint64) MarshalXML(e *xml.Encoder, start xml.StartElement) error

MarshalXML marshals the value being wrapped to XML. If there is no vale being wrapped, the zero value of its type is marshaled.

func (Uint64) String Uses

func (o Uint64) String() string

String returns the string representation of the wrapped value, or the string representation of the zero value of the type wrapped if there is no value wrapped by this optional.

func (*Uint64) UnmarshalJSON Uses

func (o *Uint64) UnmarshalJSON(data []byte) error

UnmarshalJSON unmarshals the JSON into a value wrapped by this optional.

func (*Uint64) UnmarshalXML Uses

func (o *Uint64) UnmarshalXML(d *xml.Decoder, start xml.StartElement) error

UnmarshalXML unmarshals the XML into a value wrapped by this optional.

type Uint8 Uses

type Uint8 optionalUint8

Optional wraps a value that may or may not be nil. If a value is present, it may be unwrapped to expose the underlying value.

func EmptyUint8 Uses

func EmptyUint8() Uint8

Empty returns an empty optional.

func OfUint8 Uses

func OfUint8(value uint8) Uint8

Of wraps the value in an optional.

func OfUint8Ptr Uses

func OfUint8Ptr(ptr *uint8) Uint8

func (Uint8) Else Uses

func (o Uint8) Else(elseValue uint8) (value uint8)

Else returns the value wrapped by this optional, or the value passed in if there is no value wrapped by this optional.

func (Uint8) ElseFunc Uses

func (o Uint8) ElseFunc(f func() uint8) (value uint8)

func (Uint8) ElseZero Uses

func (o Uint8) ElseZero() (value uint8)

ElseZero returns the value wrapped by this optional, or the zero value of the type wrapped if there is no value wrapped by this optional.

func (Uint8) Get Uses

func (o Uint8) Get() (value uint8, ok bool)

Get returns the value wrapped by this optional, and an ok signal for whether a value was wrapped.

func (Uint8) If Uses

func (o Uint8) If(f func(value uint8))

If calls the function if there is a value wrapped by this optional.

func (Uint8) IsPresent Uses

func (o Uint8) IsPresent() bool

IsPresent returns true if there is a value wrapped by this optional.

func (Uint8) MarshalJSON Uses

func (o Uint8) MarshalJSON() (data []byte, err error)

MarshalJSON marshals the value being wrapped to JSON. If there is no vale being wrapped, the zero value of its type is marshaled.

func (Uint8) MarshalXML Uses

func (o Uint8) MarshalXML(e *xml.Encoder, start xml.StartElement) error

MarshalXML marshals the value being wrapped to XML. If there is no vale being wrapped, the zero value of its type is marshaled.

func (Uint8) String Uses

func (o Uint8) String() string

String returns the string representation of the wrapped value, or the string representation of the zero value of the type wrapped if there is no value wrapped by this optional.

func (*Uint8) UnmarshalJSON Uses

func (o *Uint8) UnmarshalJSON(data []byte) error

UnmarshalJSON unmarshals the JSON into a value wrapped by this optional.

func (*Uint8) UnmarshalXML Uses

func (o *Uint8) UnmarshalXML(d *xml.Decoder, start xml.StartElement) error

UnmarshalXML unmarshals the XML into a value wrapped by this optional.

type Uintptr Uses

type Uintptr optionalUintptr

Optional wraps a value that may or may not be nil. If a value is present, it may be unwrapped to expose the underlying value.

func EmptyUintptr Uses

func EmptyUintptr() Uintptr

Empty returns an empty optional.

func OfUintptr Uses

func OfUintptr(value uintptr) Uintptr

Of wraps the value in an optional.

func OfUintptrPtr Uses

func OfUintptrPtr(ptr *uintptr) Uintptr

func (Uintptr) Else Uses

func (o Uintptr) Else(elseValue uintptr) (value uintptr)

Else returns the value wrapped by this optional, or the value passed in if there is no value wrapped by this optional.

func (Uintptr) ElseFunc Uses

func (o Uintptr) ElseFunc(f func() uintptr) (value uintptr)

func (Uintptr) ElseZero Uses

func (o Uintptr) ElseZero() (value uintptr)

ElseZero returns the value wrapped by this optional, or the zero value of the type wrapped if there is no value wrapped by this optional.

func (Uintptr) Get Uses

func (o Uintptr) Get() (value uintptr, ok bool)

Get returns the value wrapped by this optional, and an ok signal for whether a value was wrapped.

func (Uintptr) If Uses

func (o Uintptr) If(f func(value uintptr))

If calls the function if there is a value wrapped by this optional.

func (Uintptr) IsPresent Uses

func (o Uintptr) IsPresent() bool

IsPresent returns true if there is a value wrapped by this optional.

func (Uintptr) MarshalJSON Uses

func (o Uintptr) MarshalJSON() (data []byte, err error)

MarshalJSON marshals the value being wrapped to JSON. If there is no vale being wrapped, the zero value of its type is marshaled.

func (Uintptr) MarshalXML Uses

func (o Uintptr) MarshalXML(e *xml.Encoder, start xml.StartElement) error

MarshalXML marshals the value being wrapped to XML. If there is no vale being wrapped, the zero value of its type is marshaled.

func (Uintptr) String Uses

func (o Uintptr) String() string

String returns the string representation of the wrapped value, or the string representation of the zero value of the type wrapped if there is no value wrapped by this optional.

func (*Uintptr) UnmarshalJSON Uses

func (o *Uintptr) UnmarshalJSON(data []byte) error

UnmarshalJSON unmarshals the JSON into a value wrapped by this optional.

func (*Uintptr) UnmarshalXML Uses

func (o *Uintptr) UnmarshalXML(d *xml.Decoder, start xml.StartElement) error

UnmarshalXML unmarshals the XML into a value wrapped by this optional.

Directories

PathSynopsis
template

Package optional imports 4 packages (graph). Updated 2017-08-31. Refresh now. Tools for package owners.