Strings become useful when you can join them. Today you evaluate + on two strings, producing a new string object with their contents concatenated.
Evaluate the + operator on two string operands into a concatenated string object.
You parsed string literals back in the parsing chapter, but the evaluator has not
had a String object until now. Define it - a wrapper over the text with the
usual Type/Inspect - and teach the infix evaluator one new case: when both
operands are strings and the operator is +, produce a new string with their
contents joined.
Only + is defined on strings; -, *, and comparisons will be type errors
handled in the next lesson but one. Keep the boundary in mind - "" + "x" is
"x", an empty string concatenates cleanly - and resist adding more string
operations today. Concatenation alone makes strings first-class enough to build
messages and, later, to see printed output from your programs.
type String struct { Value string }func (s *String) Type() string { return "STRING" }// in the infix evaluator, when both operands are strings and op is "+":// return &String{ left.Value + right.Value }// other operators on strings will be a type error later