DEV Community

Cover image for Want to output a tree in Go?
Ochi Daiki
Ochi Daiki

Posted on

Want to output a tree in Go?

I am sure that you have often used the tree command to output a tree of directories.
But have you ever wanted to output a tree of something other than your local directory?
There is a Go package that you should know about, and that is https://github.com/ddddddO/gtree. This is the perfect library to easily output a tree.

Here is a simple Go program that outputs a tree from the find command.

package main

import (
    "bufio"
    "fmt"
    "os"
    "strings"

    "github.com/ddddddO/gtree"
)

// Example:
// $ cd github.com/ddddddO/gtree
// $ find . -type d -name .git -prune -o -type f -print
// ./config.go
// ./node_generator_test.go
// ./example/like_cli/adapter/indentation.go
// ./example/like_cli/adapter/executor.go
// ./example/like_cli/main.go
// ./example/find_pipe_programmable-gtree/main.go
// ...
// $ find . -type d -name .git -prune -o -type f -print | go run example/find_pipe_programmable-gtree/main.go
// << See "Output:" below. >>
func main() {
    var (
        root *gtree.Node
        node *gtree.Node
    )
    scanner := bufio.NewScanner(os.Stdin)
    for scanner.Scan() {
        line := scanner.Text()              // e.g.) "./example/find_pipe_programmable-gtree/main.go"
        splited := strings.Split(line, "/") // e.g.) [. example find_pipe_programmable-gtree main.go]

        for i, s := range splited {
            if root == nil {
                root = gtree.NewRoot(s) // s := "."
                node = root
                continue
            }
            if i == 0 {
                continue
            }

            tmp := node.Add(s)
            node = tmp
        }
        node = root
    }

    if err := gtree.OutputProgrammably(os.Stdout, root); err != nil {
        fmt.Fprintln(os.Stderr, err)
        os.Exit(1)
    }
    // Output:
    // .
    // ├── config.go
    // ├── node_generator_test.go
    // ├── example
    // │   ├── like_cli
    // │   │   ├── adapter
    // │   │   │   ├── indentation.go
    // │   │   │   └── executor.go
    // │   │   └── main.go
    // │   ├── find_pipe_programmable-gtree
    // │   │   └── main.go
    // │   ├── go-list_pipe_programmable-gtree
    // │   │   └── main.go
    // │   └── programmable
    // │       └── main.go
    // ├── file_considerer.go
    // ├── node.go
    // ├── node_generator.go
    // ├── .gitignore
    // ...
}
Enter fullscreen mode Exit fullscreen mode

I was able to output a tree with the above program.
The program I introduced here was just a utility to display a tree of directories on the local PC after all.

However, do not be discouraged.
Developers who are not me have created CLI tools to output a tree of Amazon S3 and GCS buckets! You can find them in the following repositories.

I had no idea of displaying a tree of cloud storage. Also, the aws s3 and gcloud storage commands do not have the functionality to display a tree.
I am sure you will be happy to use these CLI tools.

I got very good inspiration from these CLI tool developers. I am also very happy that they are using the gtree package!

If you want to tree output something, you may find https://github.com/ddddddO/gtree useful.

Thanks for reading!

Top comments (0)