🔍
Grep code feedback
Think of the feedback below as PR comments on your code. I have reviewed the Grep implementation written by many developers and condensed my common PR comments below. When you implement grep in Go, these are some things you should look into. These will help you evaluate whether your solution is good enough.
Argument Parsing and configuration
How are you parsing the CLI arguments? Are you parsing them manually by doing string splitting and using a giant switch case statement? If yes, then please use either
flag.Args()
from Golang standard library or Cobra. Go is suitable for writing CLI applications, and it provides really good support for doing typical tasks like parsing flags and arguments passed to the command. Make use of these packages and refactor your code accordingly. When you use Cobra, it will force a certain structure to your code. Read more about Cobra documentation and samples to find out. By the way, Docker, Kubernetes, and many other tools use Cobra for parsing arguments. When in doubt, for real-world projects, use Cobra.
How do you represent flags passed to the grep command inside your code? Have you thought about centralizing various flags in a struct called
GrepConfig
? If you centralize all the flags in a single struct, you can easily pass that struct to the grep method instead of passing each of the flags as separate parameters. Refactor your code to use a centralized config struct to represent all configuration options of the grep command.
Ideally, you should have a function that parses all arguments and converts them into an internal
GrepConfig
struct that you can pass to other methods. Otherwise, you’ll end up withgrep
method with multiple arguments for grep command options.
Argument Parsing and configuration
How are you parsing the CLI arguments? Are you parsing them manually by doing string splitting and using a giant switch case statement? If yes, then please use either
flag.Args()
from Golang standard library or Cobra. Go is suitable for writing CLI applications, and it provides really good support for doing typical tasks like parsing flags and arguments passed to the command. Make use of these packages and refactor your code accordingly. When you use Cobra, it will force a certain structure to your code. Read more about Cobra documentation and samples to find out. By the way, Docker, Kubernetes, and many other tools use Cobra for parsing arguments. When in doubt, for real-world projects, use Cobra.
How do you represent flags passed to the grep command inside your code? Have you thought about centralizing various flags in a struct called
GrepConfig
? If you centralize all the flags in a single struct, you can easily pass that struct to the grep method instead of passing each of the flags as separate parameters. Refactor your code to use a centralized config struct to represent all configuration options of the grep command.
Ideally, you should have a function that parses all arguments and converts them into an internal
GrepConfig
struct that you can pass to other methods. Otherwise, you’ll end up withgrep
method with multiple arguments for grep command options.
Argument Parsing and configuration
How are you parsing the CLI arguments? Are you parsing them manually by doing string splitting and using a giant switch case statement? If yes, then please use either
flag.Args()
from Golang standard library or Cobra. Go is suitable for writing CLI applications, and it provides really good support for doing typical tasks like parsing flags and arguments passed to the command. Make use of these packages and refactor your code accordingly. When you use Cobra, it will force a certain structure to your code. Read more about Cobra documentation and samples to find out. By the way, Docker, Kubernetes, and many other tools use Cobra for parsing arguments. When in doubt, for real-world projects, use Cobra.
How do you represent flags passed to the grep command inside your code? Have you thought about centralizing various flags in a struct called
GrepConfig
? If you centralize all the flags in a single struct, you can easily pass that struct to the grep method instead of passing each of the flags as separate parameters. Refactor your code to use a centralized config struct to represent all configuration options of the grep command.
Ideally, you should have a function that parses all arguments and converts them into an internal
GrepConfig
struct that you can pass to other methods. Otherwise, you’ll end up withgrep
method with multiple arguments for grep command options.
Argument Parsing and configuration
How are you parsing the CLI arguments? Are you parsing them manually by doing string splitting and using a giant switch case statement? If yes, then please use either
flag.Args()
from Golang standard library or Cobra. Go is suitable for writing CLI applications, and it provides really good support for doing typical tasks like parsing flags and arguments passed to the command. Make use of these packages and refactor your code accordingly. When you use Cobra, it will force a certain structure to your code. Read more about Cobra documentation and samples to find out. By the way, Docker, Kubernetes, and many other tools use Cobra for parsing arguments. When in doubt, for real-world projects, use Cobra.
How do you represent flags passed to the grep command inside your code? Have you thought about centralizing various flags in a struct called
GrepConfig
? If you centralize all the flags in a single struct, you can easily pass that struct to the grep method instead of passing each of the flags as separate parameters. Refactor your code to use a centralized config struct to represent all configuration options of the grep command.
Ideally, you should have a function that parses all arguments and converts them into an internal
GrepConfig
struct that you can pass to other methods. Otherwise, you’ll end up withgrep
method with multiple arguments for grep command options.
Argument Parsing and configuration
How are you parsing the CLI arguments? Are you parsing them manually by doing string splitting and using a giant switch case statement? If yes, then please use either
flag.Args()
from Golang standard library or Cobra. Go is suitable for writing CLI applications, and it provides really good support for doing typical tasks like parsing flags and arguments passed to the command. Make use of these packages and refactor your code accordingly. When you use Cobra, it will force a certain structure to your code. Read more about Cobra documentation and samples to find out. By the way, Docker, Kubernetes, and many other tools use Cobra for parsing arguments. When in doubt, for real-world projects, use Cobra.
How do you represent flags passed to the grep command inside your code? Have you thought about centralizing various flags in a struct called
GrepConfig
? If you centralize all the flags in a single struct, you can easily pass that struct to the grep method instead of passing each of the flags as separate parameters. Refactor your code to use a centralized config struct to represent all configuration options of the grep command.
Ideally, you should have a function that parses all arguments and converts them into an internal
GrepConfig
struct that you can pass to other methods. Otherwise, you’ll end up withgrep
method with multiple arguments for grep command options.
Code formatting and tooling
Code formatting and tooling
Code formatting and tooling
Code formatting and tooling
Code formatting and tooling
Unit testing and error handling
Unit testing and error handling
Unit testing and error handling
Unit testing and error handling
Unit testing and error handling
Power of interfaces
Power of interfaces
Power of interfaces
Power of interfaces
Power of interfaces
Performance (when running wc on many large files)
Performance (when running wc on many large files)
Performance (when running wc on many large files)
Performance (when running wc on many large files)
Performance (when running wc on many large files)
Handling Results
Handling Results
Handling Results
Handling Results
Handling Results
Others (Misc)
Others (Misc)
Others (Misc)
Others (Misc)
Others (Misc)