Creating event handlers
The HandleFunc
type we defined in the last sections handles the core of our functionality. This is also where we decide on how we want to turn a bunch of text into a command to run.
There are a few ways to interpret raw text:
- Regexes via the
regexp
package - String manipulation via the
strings
package - Designing or using a lexer and parser
Regexes and string manipulation are the fastest ways for an application of this type where we have single lines of text.
Lexers and parsers are great when you need to deal with complex inputs or multi-line text and cannot afford mistakes. This is the method that compilers use to read your textual code into instructions that eventually lead to a compiled binary. Rob Pike has a great talk on writing one in Go that you can view here: https://www.youtube.com/watch?v=HxaD_trXwRE. The downside is that they are tedious to build and hard to train new people on. If you need to watch that video a few times...