Skip to content

How do I check for null values? #9

@jmcmahon

Description

@jmcmahon

How do I check if the address field below is null?

package main

import (
	"fmt"
	"github.com/smallfish/simpleyaml"
)

var data = []byte(`
name: smallfish
age: 99
bool: true
bb:
    cc:
        dd:
            - 111
            - 222
            - 333
address: null
`)

func main() {
	y, err := simpleyaml.NewYaml(data)
	if err != nil {
		// ERROR
	}

	if v, err := y.Get("name").String(); err == nil {
		fmt.Println("value:", v)
	}

  if v, err := y.Get("age").Int(); err == nil {
    fmt.Println("value:", v)
  }

  if v, err := y.Get("bool").Bool(); err == nil {
    fmt.Println("value:", v)
  }
  
  if v, err := y.Get("bb").Get("cc").Get("dd").Array() ; err == nil {
    fmt.Println("value:", v)
  }

  if v, err := y.Get("bb").Get("cc").Get("dd").GetIndex(1).Int() ; err == nil {
    fmt.Println("value:", v)
  }

  if v, err := y.GetPath("bb", "cc", "dd").Array() ; err == nil {
    fmt.Println("value:", v)
  }


}

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions