Created
March 16, 2021 10:52
-
-
Save nkanaev/8e1deee0be8dee4e1ec63d230549d0ad to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// show go module dependencies as an indented tree | |
// usage: go mod graph | go run gt.go | |
package main | |
import ( | |
"bufio" | |
"fmt" | |
"os" | |
"strings" | |
) | |
func show(root string, deps map[string][]string, depth int) { | |
fmt.Println(strings.Repeat("\t", depth), root) | |
for _, dep := range deps[root] { | |
show(dep, deps, depth + 1) | |
} | |
} | |
func main() { | |
root := "" | |
deps := make(map[string][]string) | |
scanner := bufio.NewScanner(os.Stdin) | |
for scanner.Scan() { | |
parts := strings.Split(scanner.Text(), " ") | |
if len(parts) != 2 { | |
continue | |
} | |
l, r := parts[0], parts[1] | |
if root == "" { | |
root = l | |
} | |
if deps[l] == nil { | |
deps[l] = make([]string, 0) | |
} | |
deps[l] = append(deps[l], r) | |
} | |
show(root, deps, 0) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment