Adding a Helpers Package
Explore how to add a helpers package containing reusable functions like CreateDir and AddFiles to improve code organization and testing in Go command-line programs. This lesson helps you refactor and modularize your code for better maintainability and easier manual acceptance testing.
We'll cover the following...
We'll cover the following...
The helpers package
In addition to the repo_manager package, let’s add a little helpers package. This package will contain some general functions that can be used later by various tests. Multi-git deals with files and directories managed by git. Two useful functions are CreateDir() and AddFiles().
The CreateDir() function creates a directory and optionally initializes git in that directory:
func CreateDir(baseDir string, name string, initGit bool) (err error) {
dirName := path.Join(baseDir, name)
err = os.MkdirAll(dirName, os.ModePerm)
if err != nil {
return
}
if !initGit {
return
}
currDir, err := os.Getwd()
if err != nil {
return
}
defer ...