GO PKG

fmt

  • %v vs %#v

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    type Example struct {
    Name string
    Value int
    }

    func main() {
    ex := Example{Name: "test", Value: 42}

    // Using %v
    fmt.Printf("Default format: %v\n", ex)
    // Output: Default format: {test 42}

    // Using %#v
    fmt.Printf("Go syntax representation: %#v\n", ex)
    // Output: Go syntax representation: main.Example{Name:"test", Value:42}
    }

json

  • json:",inline"

    This means that the fields of the embedded struct will appear at the same level as the fields of the parent struct in the resulting JSON object.

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    package main

    import (
    "encoding/json"
    "fmt"
    )

    type Meta struct {
    Name string `json:"name"`
    Age int `json:"age"`
    }

    type Person struct {
    Meta `json:",inline"`
    Address string `json:"address"`
    }

    func main() {
    p := Person{
    Meta: Meta{
    Name: "John",
    Age: 30,
    },
    Address: "123 Main St",
    }

    data, _ := json.Marshal(p)
    fmt.Println(string(data))
    }

    # output:
    {
    "name": "John",
    "age": 30,
    "address": "123 Main St"
    }

cobra

PersistentFlag vs Flag

PersistentFlag

  • Flags that are available to the command and all its subcommands. but not explicitly added to the main command’s help output.

Flag

  • Definition: Flags that are only available to the specific command they are defined on.

  • Example: If you set a flag on a specific command, it will not be available to its parent or sibling commands.