Popularity
2.0
Growing
Activity
0.0
Stable
18
3
1

Programming language: Go
License: Apache License 2.0
Latest version: v1.0.2

go-xray alternatives and similar packages

Based on the "Generation and Generics" category.
Alternatively, view go-xray alternatives based on common mentions on social networks and blogs.

Do you think we are missing an alternative of go-xray or a related project?

Add another 'Generation and Generics' Package

README

go-xray

Go Report Card Documentation license GitHub version GitHub issues

This is a Golang library with reflection related functions which I use in my different projects.

KeyValue

This type is used to construct a key-value pair. You can use it as follows:

package main

import (
    "fmt"

    "https://github.com/pieterclaerhout/go-xray"
)

func main() {

    pair := &xray.KeyValue{
        Key:   "key",
        Value: "value",
    }

    fmt.Println(pair.String())
    // Ouptut: key=value

}

If both Key and Value are empty, an empty string is returned.

Name

Name allows you to get the name of an object.

package main

import (
    "fmt"

    "https://github.com/pieterclaerhout/go-xray"
)

func main() {

    v := &test{}

    fmt.Println(xray.Name(v))
    // Ouptut: test

    n := nil

    fmt.Println(xray.Name(n))
    // Ouptut: <nil>

}

Properties

The function xray.Properties returns the a list with the property names defined in a struct:

package main

import (
    "fmt"

    "https://github.com/pieterclaerhout/go-xray"
)

func main() {

    type sampleStruct struct {
        Name  string `form:"name" json:"name"`
        Title string `form:"title" json:"title"`
    }

    v, _ := xray.Properties(sampleStruct{})

    // v now contains:
    // []string{
    //     "Name",
    //     "Title",
    // }

}

The function xray.PropertiesAsMap does the same, but also includes the values and returns a map[string]interface{} instance:

package main

import (
    "fmt"

    "https://github.com/pieterclaerhout/go-xray"
)

func main() {

    type sampleStruct struct {
        Name  string `form:"name" json:"name"`
        Title string `form:"title" json:"title"`
    }

    v, _ := xray.PropertiesAsMap(sampleStruct{})

    // v now contains:
    // map[string]interface{}{
    //     "Name": "name",
    //     "Title": "title",
    // }

}

The function xray.Property retrieves a single value by it's name:

package main

import (
    "fmt"

    "https://github.com/pieterclaerhout/go-xray"
)

func main() {

    type sampleStruct struct {
        Name  string `form:"name" json:"name"`
        Title string `form:"title" json:"title"`
    }

    v, _ := xray.Property(sampleStruct{}, "Name")

    // v now contains:
    // "name"

}

Tags

The xray.Tags function allows you to easily extract tags from a struct:

package main

import (
    "fmt"

    "https://github.com/pieterclaerhout/go-xray"
)

func main() {

    type sampleStruct struct {
        Name  string `form:"name" json:"name"`
        Title string `form:"title" json:"title"`
    }

    v, _ := xray.Tags(sampleStruct{}, "form")

    // v now contains:
    // map[string]string{
    //     "Name": "name",
    //     "Title": "title",
    // }

}


*Note that all licence references and agreements mentioned in the go-xray README section above are relevant to that project's source code only.