Skip to content

Instantly share code, notes, and snippets.

@vidhill
Last active February 3, 2022 14:46
Show Gist options
  • Select an option

  • Save vidhill/bc7297c1672b600b8c5f395cbf611047 to your computer and use it in GitHub Desktop.

Select an option

Save vidhill/bc7297c1672b600b8c5f395cbf611047 to your computer and use it in GitHub Desktop.

Revisions

  1. vidhill revised this gist Feb 3, 2022. No changes.
  2. vidhill revised this gist Feb 3, 2022. 2 changed files with 3 additions and 2 deletions.
    2 changes: 0 additions & 2 deletions main.go
    Original file line number Diff line number Diff line change
    @@ -5,7 +5,6 @@ import (
    )

    func main() {

    doggo := Dog{
    Name: "Fluffy",
    }
    @@ -18,5 +17,4 @@ func main() {
    }

    fmt.Println(fieldName)

    }
    3 changes: 3 additions & 0 deletions util.go
    Original file line number Diff line number Diff line change
    @@ -1,3 +1,4 @@
    // main logic sourced from https://stackoverflow.com/a/69061124
    package main

    import (
    @@ -14,6 +15,8 @@ func getStructTag(f reflect.StructField, tagName string) string {
    return string(f.Tag.Get(tagName))
    }

    // based on the function used in the go standard library "encoding/json" source
    // https://cs.opensource.google/go/go/+/refs/tags/go1.17.6:src/encoding/json/tags.go;l=17
    func extractFieldName(tag string) string {
    if idx := strings.Index(tag, ","); idx != -1 {
    return tag[:idx]
  3. vidhill created this gist Feb 3, 2022.
    6 changes: 6 additions & 0 deletions go.mod
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,6 @@
    module dummy/foo

    go 1.14

    require (
    )
    22 changes: 22 additions & 0 deletions main.go
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,22 @@
    package main

    import (
    "fmt"
    )

    func main() {

    doggo := Dog{
    Name: "Fluffy",
    }

    fieldName, e := getJsonTagValue(doggo, "Name")

    if e != nil {
    fmt.Println(e.Error())
    return
    }

    fmt.Println(fieldName)

    }
    39 changes: 39 additions & 0 deletions util.go
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,39 @@
    package main

    import (
    "fmt"
    "reflect"
    "strings"
    )

    type Dog struct {
    Name string `json:"bar,omitempty"`
    }

    func getStructTag(f reflect.StructField, tagName string) string {
    return string(f.Tag.Get(tagName))
    }

    func extractFieldName(tag string) string {
    if idx := strings.Index(tag, ","); idx != -1 {
    return tag[:idx]
    }
    return tag
    }

    func GetStructField(f Dog, fieldName string) (reflect.StructField, error) {
    field, ok := reflect.TypeOf(&f).Elem().FieldByName(fieldName)
    if !ok {
    emptyValue := reflect.StructField{}
    return emptyValue, fmt.Errorf(`the field with the name "%s" was not found`, fieldName)
    }
    return field, nil
    }

    // Returns the json tag for a given struct field
    func getJsonTagValue(f Dog, fieldName string) (string, error) {
    field, err := GetStructField(f, fieldName)

    tag := getStructTag(field, "json")
    return extractFieldName(tag), err
    }