Skip to main content

Go Example

package main

type component interface {
search(string)
}
package main

import "fmt"

type file struct {
name string
}

func (f *file) search(keyword string) {
fmt.Printf("Searching for keyword %s in file %s\n", keyword, f.name)
}

func (f *file) getName() string {
return f.name
}
package main

import "fmt"

type folder struct {
components []component
name string
}

func (f *folder) search(keyword string) {
fmt.Printf("Serching recursively for keyword %s in folder %s\n", keyword, f.name)
for _, composite := range f.components {
composite.search(keyword)
}
}

func (f *folder) add(c component) {
f.components = append(f.components, c)
}
package main

func main() {
file1 := &file{name: "File1"}
file2 := &file{name: "File2"}
file3 := &file{name: "File3"}

folder1 := &folder{
name: "Folder1",
}

folder1.add(file1)

folder2 := &folder{
name: "Folder2",
}
folder2.add(file2)
folder2.add(file3)
folder2.add(folder1)

folder2.search("rose")
}
Serching recursively for keyword rose in folder Folder2
Searching for keyword rose in file File2
Searching for keyword rose in file File3
Serching recursively for keyword rose in folder Folder1
Searching for keyword rose in file File1