With all the examples out there and a little experimentation, I found the solution I needed. Here's the tree from the 'top' directory:
├── dfs
│ ├── dfs.go
│ ├── dfs_test.go
│ └── go.mod
├── first.go
├── go.mod
└── ModuleDFS
Here are the file contents starting from the top of the directory structure on down:
go.mod:
======
module ModuleDFS
require _/dfs v0.0.0
replace _/dfs => ./dfs
go 1.14
first.go:
======
package main
import (
"bufio"
"fmt"
"log"
"os"
"strings"
"_/dfs"
)
func main() {
gr := make(map[string][]string)
// vertex --^ ^-- slice of vertices adjacent to this vertex
scanner := bufio.NewScanner(os.Stdin)
for scanner.Scan() {
in := scanner.Text()
// in: A B C etc.
// Ctrl-D at end!
flds := strings.Fields(in) // split line into fields
vs := make([]string, 0)
// set up vertices connected to first vertex at index 0
for f := 1; f < len(flds); f++ {
vs = append(vs, flds[f])
}
gr[flds[0]] = vs
}
if err := scanner.Err(); err != nil {
log.Fatalln("Error scanning:", err)
}
for k, v := range gr {
fmt.Printf("%s:%v\n", k, v)
}
dfs.SetupDfs(gr)
}
// check simply checks for an error w/o inline code everywhere
// Also, since a fatal error occurred, this is no slowdown!
func check(why string, err error) {
if err != nil {
log.Fatalln(why, ":", err)
}
}
Directory dfs:
==========
go.mod:
======
module _/ModuleDFS/dfs
go 1.14
dfs.go:
=====
package dfs
import "fmt"
// setupDFS initialized dfs variables
func SetupDfs(gr map[string][]string) {
nT := 0 // Total nodes in whole graph
for n := range gr {
nT += len(gr[n])
}
visited := make(map[string]bool, nT)
fmt.Println("Total nodes:", nT, "Visited:", visited)
dfs(gr, "a", visited)
fmt.Println("Final:", visited)
}
// dfs peforms a Depth First Search of all nodes
func dfs(gr map[string][]string, at string,
vs map[string]bool) map[string]bool {
if vs[at] == true {
return vs
}
vs[at] = true
neighbors := gr[at]
fmt.Println("neighbors[", at, "]:", gr[at])
for _, next := range neighbors {
fmt.Println("next:", next)
dfs(gr, next, vs)
}
return vs
}
// Hello -- the most basic of testing
func Hello() string {
return "Hello World"
}
dfs_test.go:
=========
package dfs
import "testing"
func TestDfs(t *testing.T) {
want := "Hello World"
if got := Hello(); got != want {
t.Errorf("Hello() = %q, want %q", got, want)
}
}
Thanks for your help!