patterngoMinor
Command called 'sprinkle'
Viewed 0 times
sprinklecommandcalled
Problem
I have a command called
Here is what I need help with:
```
// The sprinkle command allows the user to define some transformations in a file called test.txt.
// The user can then provide some domain names such as 'go' and the application will randomly transform it, using the transformations defined in test.txt.
// Every transformation in the test.txt file must have an asterisk (*) in place of the domain name. For example, if the test file contains:
//
// *-is-awesome.com
//
// love-*.net
//
// lets-*.org
//
// Then, if the user enters 'go' in the command line, the output could be something like:
//
// go-is-awesome.com
package main
import (
"bufio"
"fmt"
"log"
"math/rand"
"os"
"strings"
"time"
)
const (
domainName = "*"
)
var transformations []string
func main() {
var err error
transformations, err = readLinesFromFile("test.txt")
if err != nil {
log.Fatal(err)
}
inputScanner := bufio.NewScanner(os.Stdin)
for inputScanner.Scan() {
fmt.Println(applyRandomTransformation(inputScanner.Text()))
}
}
// readLinesFromFile reads a file into an array. Each element in the array is one line of the text file.
func readLinesFromFile(path string) ([]string, error) {
var lines []string
file, err := os.Open("test.txt")
if err != nil {
return nil, err
}
defer file.Close()
scanner := bufio.NewScanner(file)
for scanner.Scan() {
lines = append(lines, scanner.Text())
}
return lines, nil
}
// applyRando
sprinkle. I'm not going to explain anything about it, because I want whoever that reviews the code to also tell me how much of the command documentation they understood.Here is what I need help with:
- Is the documentation good enough? Is it too verbose?
- Does the constant
domainNamehelp you understand the code below, or is it useless?
- Should the
transformationsarray be passed around instead of being defined as a global variable?
- What else can I improve in the code?
```
// The sprinkle command allows the user to define some transformations in a file called test.txt.
// The user can then provide some domain names such as 'go' and the application will randomly transform it, using the transformations defined in test.txt.
// Every transformation in the test.txt file must have an asterisk (*) in place of the domain name. For example, if the test file contains:
//
// *-is-awesome.com
//
// love-*.net
//
// lets-*.org
//
// Then, if the user enters 'go' in the command line, the output could be something like:
//
// go-is-awesome.com
package main
import (
"bufio"
"fmt"
"log"
"math/rand"
"os"
"strings"
"time"
)
const (
domainName = "*"
)
var transformations []string
func main() {
var err error
transformations, err = readLinesFromFile("test.txt")
if err != nil {
log.Fatal(err)
}
inputScanner := bufio.NewScanner(os.Stdin)
for inputScanner.Scan() {
fmt.Println(applyRandomTransformation(inputScanner.Text()))
}
}
// readLinesFromFile reads a file into an array. Each element in the array is one line of the text file.
func readLinesFromFile(path string) ([]string, error) {
var lines []string
file, err := os.Open("test.txt")
if err != nil {
return nil, err
}
defer file.Close()
scanner := bufio.NewScanner(file)
for scanner.Scan() {
lines = append(lines, scanner.Text())
}
return lines, nil
}
// applyRando
Solution
First, look at your documentation to see how will look to users:
The Go Blog
Godoc: documenting Go code
Amongst other things, domain name is a misnomer.
Second, look at your
Third, read your code. And, fourth, run your code.
Find and fix several bugs (others have identified some of them). Since
Rewrite the
TODO: provide
The Go Blog
Godoc: documenting Go code
$ go doc sprinkle
The sprinkle command allows the user to define some transformations in a
file called test.txt. The user can then provide some domain names such as
'go' and the application will randomly transform it, using the
transformations defined in test.txt. Every transformation in the test.txt
file must have an asterisk (*) in place of the domain name. For example, if
the test file contains:
*-is-awesome.com
love-*.net
lets-*.org
Then, if the user enters 'go' in the command line, the output could be
something like:
go-is-awesome.com
$Amongst other things, domain name is a misnomer.
Second, look at your
sprinkle command help:$ ./sprinkle -help
$Third, read your code. And, fourth, run your code.
Find and fix several bugs (others have identified some of them). Since
domainName is a misnomer, rename it to wildcard. Rewrite the
sprinkle command go doc description:$ go doc
The sprinkle command transforms a random line of text from the sprinkle.txt
file by replacing asterisk (*) wildcard characters with text from stdin.
For example,
$ cat sprinkle.txt
*-is-awesome.com
love-*.net
lets-*.org
$ echo go | ./sprinkle
love-go.net
$ echo go | ./sprinkle
go-is-awesome.com
$
$TODO: provide
sprinkle command help.Code Snippets
$ go doc sprinkle
The sprinkle command allows the user to define some transformations in a
file called test.txt. The user can then provide some domain names such as
'go' and the application will randomly transform it, using the
transformations defined in test.txt. Every transformation in the test.txt
file must have an asterisk (*) in place of the domain name. For example, if
the test file contains:
*-is-awesome.com
love-*.net
lets-*.org
Then, if the user enters 'go' in the command line, the output could be
something like:
go-is-awesome.com
$$ ./sprinkle -help
$$ go doc
The sprinkle command transforms a random line of text from the sprinkle.txt
file by replacing asterisk (*) wildcard characters with text from stdin.
For example,
$ cat sprinkle.txt
*-is-awesome.com
love-*.net
lets-*.org
$ echo go | ./sprinkle
love-go.net
$ echo go | ./sprinkle
go-is-awesome.com
$
$Context
StackExchange Code Review Q#118964, answer score: 3
Revisions (0)
No revisions yet.