DEV Community

Ochi Daiki
Ochi Daiki

Posted on • Updated on

Can generate a Tree by Golang!

please go to https://github.com/ddddddO/gtree

There are three ways to generate tree (CLI, Package(1), Package(2)). They are explained below.

CLI and Package(1).

Given a Markdown file or format, the result of linux tree command is printed.

Hierarchy is represented by hyphen and indentation.

Indentation should be unified by one of the following.
・Tab (default)
・Two spaces (required: -ts)
・Four spaces (required: -fs)

Package(1)

You can customize branch format.

Package(2)

You can also generate a tree programmatically.
Markdown is irrelevant.
You can customize branch format.

Discussion (2)

Collapse
ddddddo profile image
Ochi Daiki Author

Below is a sample of program.

package main

import (
    "os"

    "github.com/ddddddO/gtree/v6"
)

func main() {
    var root *gtree.Node = gtree.NewRoot("root")
    root.Add("child 1").Add("child 2").Add("child 3")
    var child4 *gtree.Node = root.Add("child 1").Add("child 2").Add("child 4")
    child4.Add("child 5")
    child4.Add("child 6").Add("child 7")
    root.Add("child 8")
    // you can customize branch format.
    if err := gtree.ExecuteProgrammably(os.Stdout, root,
        gtree.BranchFormatIntermedialNode("+--", ":   "),
        gtree.BranchFormatLastNode("+--", "    "),
    ); err != nil {
        panic(err)
    }
    // Output:
    // root
    // +-- child 1
    // :   +-- child 2
    // :       +-- child 3
    // :       +-- child 4
    // :           +-- child 5
    // :           +-- child 6
    // :               +-- child 7
    // +-- child 8

    primate := preparePrimate()
    // default branch format.
    if err := gtree.ExecuteProgrammably(os.Stdout, primate); err != nil {
        panic(err)
    }
    // Output:
    // Primate
    // ├── Strepsirrhini
    // │   ├── Lemuriformes
    // │   │   ├── Lemuroidea
    // │   │   │   ├── Cheirogaleidae
    // │   │   │   ├── Indriidae
    // │   │   │   ├── Lemuridae
    // │   │   │   └── Lepilemuridae
    // │   │   └── Daubentonioidea
    // │   │       └── Daubentoniidae
    // │   └── Lorisiformes
    // │       ├── Galagidae
    // │       └── Lorisidae
    // └── Haplorrhini
    //     ├── Tarsiiformes
    //     │   └── Tarsiidae
    //     └── Simiiformes
    //         ├── Platyrrhini
    //         │   ├── Ceboidea
    //         │   │   ├── Atelidae
    //         │   │   └── Cebidae
    //         │   └── Pithecioidea
    //         │       └── Pitheciidae
    //         └── Catarrhini
    //             ├── Cercopithecoidea
    //             │   └── Cercopithecidae
    //             └── Hominoidea
    //                 ├── Hylobatidae
    //                 └── Hominidae
}

func preparePrimate() *gtree.Node {
    primate := gtree.NewRoot("Primate")
    strepsirrhini := primate.Add("Strepsirrhini")
    haplorrhini := primate.Add("Haplorrhini")
    lemuriformes := strepsirrhini.Add("Lemuriformes")
    lorisiformes := strepsirrhini.Add("Lorisiformes")

    lemuroidea := lemuriformes.Add("Lemuroidea")
    lemuroidea.Add("Cheirogaleidae")
    lemuroidea.Add("Indriidae")
    lemuroidea.Add("Lemuridae")
    lemuroidea.Add("Lepilemuridae")

    lemuriformes.Add("Daubentonioidea").Add("Daubentoniidae")

    lorisiformes.Add("Galagidae")
    lorisiformes.Add("Lorisidae")

    haplorrhini.Add("Tarsiiformes").Add("Tarsiidae")
    simiiformes := haplorrhini.Add("Simiiformes")

    platyrrhini := haplorrhini.Add("Platyrrhini")
    ceboidea := platyrrhini.Add("Ceboidea")
    ceboidea.Add("Atelidae")
    ceboidea.Add("Cebidae")
    platyrrhini.Add("Pithecioidea").Add("Pitheciidae")

    catarrhini := simiiformes.Add("Catarrhini")
    catarrhini.Add("Cercopithecoidea").Add("Cercopithecidae")
    hominoidea := catarrhini.Add("Hominoidea")
    hominoidea.Add("Hylobatidae")
    hominoidea.Add("Hominidae")

    return primate
}

Enter fullscreen mode Exit fullscreen mode
Collapse
ddddddo profile image
Ochi Daiki Author

Below is a sample of cli.

20:25:28 > gtree -ts << EOS
> - a
>   - vvv
>     - jjj
>   - kggg
>     - kkdd
>     - tggg
>   - edddd
>     - orrr
>   - gggg
> EOS
a
├── vvv
│   └── jjj
├── kggg
│   ├── kkdd
│   └── tggg
├── edddd
│   └── orrr
└── gggg
Enter fullscreen mode Exit fullscreen mode