Skip to content

Instantly share code, notes, and snippets.

@orian
Last active August 29, 2015 14:22
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save orian/0d5ebbd20613b0ea07c5 to your computer and use it in GitHub Desktop.
Save orian/0d5ebbd20613b0ea07c5 to your computer and use it in GitHub Desktop.
pattern name generator
package main
import (
"fmt"
"path"
)
type StringGenerator interface {
Next() bool
Get() string
}
type NameGen struct {
basePath string
baseFile string
start int // [start, end)
end int
num int
withoutIdx bool
current string
}
func NewNameGen(base, file string, start, num int, withoutIdx bool) *NameGen {
return &NameGen{base, file, start, start + num, num, withoutIdx, ""}
}
func (n *NameGen) Next() bool {
if n.withoutIdx {
n.current = path.Join(n.basePath, fmt.Sprintf("%s.jpeg", n.baseFile))
n.withoutIdx = false
return true
}
if n.start < n.end {
n.current = path.Join(n.basePath, fmt.Sprintf("%s_%d.jpeg", n.baseFile, n.start))
n.start = n.start + 1
return true
}
return false
}
func (n *NameGen) Has() bool {
return len(n.current) > 0
}
func (n *NameGen) Get() string {
return n.current
}
func main() {
fmt.Println("Hello, playground")
var g StringGenerator = NewNameGen("/usr", "name", 0, 5, true)
for g.Next() {
fmt.Println(g.Get())
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment