Problem overview
The starting code prints three lines for each of three people: Andy, Mike and Sarah. Every group of three lines has the same shape: My name is ..., I code in ... and I have a .... Only the name, the language and the pet change.
The task is to write the same program in a better way. You define a function called printMessages that takes name, language and animal, and prints the three matching lines. Then mainFunction calls it three times. The output must be identical to the original nine lines.
Approach: find what repeats and what changes
Compare the three blocks of the original code. The parts that repeat are the sentence beginnings "My name is ", "I code in " and "I have a ". The parts that change are the values: Andy, Python and dog in the first block, and different values in the others.
The repeating parts go into the function body. The changing parts become parameters. Each call then supplies a different set of values. The nine fmt.Println lines turn into three prints in one function plus three short calls, and adding a fourth person costs one more line instead of three.
Step by step
- Define the function above
mainFunction. Usefunc, the nameprintMessagesand threestringparameters:name,languageandanimal. Go writes each type after its name. - Print the first line. Join
"My name is "andnamewith+. The trailing space inside the quotes matters, because+inserts nothing between its operands. - Print the second and third lines the same way, with
"I code in "andlanguage, then"I have a "andanimal. - Call the function for Andy, inside
mainFunction, with"Andy", "Python", "dog". The arguments go in the same order as the parameters. - Call it for Mike with
"Mike", "Java", "cat", and for Sarah with"Sarah", "JavaScript", "parrot".
The solution in Go
// Define & implement the function printMessages here:
func printMessages(name string, language string, animal string) {
fmt.Println("My name is " + name)
fmt.Println("I code in " + language)
fmt.Println("I have a " + animal)
}
func mainFunction() {
// Call printMessages 3 times here:
printMessages("Andy", "Python", "dog")
printMessages("Mike", "Java", "cat")
printMessages("Sarah", "JavaScript", "parrot")
}
Go details worth knowing about this solution:
- Grouped parameter types. When consecutive parameters have the same type, you can write the type once:
func printMessages(name, language, animal string). It is the same function. The longer form in the solution is just as valid. - Positional matching. Go has no named arguments. The first argument fills
name, the secondlanguageand the thirdanimal. Because all three are strings, a wrong order compiles and prints wrong text, such asMy name is dog. - Exact argument count. A call with two arguments or four is a compile error. There are no default values.
- Return type. The function declares no result, so it returns nothing. It prints, and the caller just calls it as a statement.
- Copies. Each call gets its own copy of the strings, so the three calls do not affect each other.
Edge cases and common mistakes
- Missing space.
"My name is" + nameprintsMy name isAndy. Keep the space at the end of each fixed piece. - Quoting the parameter.
"name"in quotes is the wordname. Without quotes,nameis the parameter's value. - Wrong order of calls. The output has to list Andy, Mike and Sarah in that order, so the calls must come in that order too.
- Printing the three lines of one person together. Each call must print all three lines before the next call starts. Putting all names first and then all languages would give a different output.
- Defining the function inside
mainFunction. Go does not allow a namedfuncdeclaration inside another function. Define it at the top level, abovemainFunction.
Testing
Run the program and compare it with the original. The first three lines should be:
My name is Andy
I code in Python
I have a dog
The next six lines follow the same pattern for Mike (Java, cat) and Sarah (JavaScript, parrot).