In the last lesson you learned what a string was and the pattern that all strings must follow in Go.
In this lesson you are going to practice fixing some mistakes in programs that use strings. You will also learn an approach to fixing syntax errors.
You must have completed the Words lesson before you will be able to complete this lesson.
Each letter, number or symbol on the keyboard is called a character. A string is a sequence of these letters, numbers or symbols. Anything that can by typed on a keyboard is considered a string.
Each character in every human language has a unique number. The number is defined by the Unicode character encoding. The computer can only process numbers so it works with these unique numbers, and not with the letters or symbols themselves. This means that a string is also a sequence of numbers.
Because the concept of a character is different for different languages, for example Chinese uses logograms, Go uses the term rune instead of character.
A string in Go is therefore a sequence of runes.
The letters A, z and the symbols %, * and # can all be represented by numbers. Using The Unicode Table you can find out the numbers used for these letters and symbols.
A is 65
z is 122
% is 37
* is 42
# is 35
A string in Go must be typed inside inverted commas, one at each end of the string. Like this
"Strings, I see you now"
The string must also be typed on one line. This string would be illegal.
"A horse, a horse, my kingdom for a horse"
This is a pattern. All strings in Go must follow this pattern. If the string does not, it is a syntax error.
To turn the line
I have been chosen! Farewell, my friends. I go on to a better place.
into a string, you need to surround it in inverted commas. Like this
"I have been chosen! Farewell, my friends. I go on to a better place."
There are two mistakes in this example string
I don't believe that man's ever been to medical school!"
Can you find them?
The first mistake is the string is incorrectly written on two lines. Remember a string must be typed on one line only. The second mistake is the string is missing an inverted comma at the start of the string. It would be correctly written like this
"I don't believe that man's ever been to medical school!"
Printing a string requires
the use of one of two functions. The two functions are called
fmt.Println. Printing to the screen is a type of output.
The functions are used like this
fmt.Println("First string") fmt.Print("and second string.")
These lines are called statements.
The pattern to print to the screen is this. The statement always starts with
fmt.Println. The case is significant, so the spelling must
be exactly as shown. There must be a full stop between the
fmt and the
fmt.Print functions are not the same. The
fmt.Print prints the string that you put in between the brackets. The next
fmt.Println function will continue to print on the same line.
fmt.Println function also prints the string that you put in between
the brackets but also takes a new line at the end. The next
string that you print with
fmt.Print will be printed
on the next line.
If you had typed this into a program (ignore the line numbers, they are for illustration only)
1 fmt.Println("Printed on Line 1") 2 fmt.Print("Printed on Line 2") 3 fmt.Println(" also printed on Line 2") 4 fmt.Println("Printed on Line 3")
What do you think the output would be? Can you explain why?
The output would be:
1 Printed on Line 1 2 Printed on Line 2 also printed on Line 2") 3 Printed on Line 3
fmt.Print function does take a new line at the end, so lines 3 and 4
in the program produce line 3 in the output.
fmt.Print statement does not follow the correct pattern
there is an error. This is called a syntax error. Go will not run a program
that contains any syntax errors. Your program must be free of syntax errors
Following this pattern is like following the correct spelling in English writing. If the spelling is wrong the writing will not make sense.
Here are couple of ways to find syntax errors.
Fixing a syntax error is much simpler than finding it. The line in the source code file that contains the error must be found and edited to remove the error. The source code file then needs saving again. Another attempt to run the program can now be made. If no more syntax errors exist the program will run. Otherwise you have to repeat the process with the next error.
Fixing a syntax error is not debugging. But they are related. Debugging is the act of correcting the logic of program only when it runs. A syntax error completely prevents the program from running. So fixing a syntax error is not debugging.
This is the
badstring.go program. The program contains syntax errors and will
not run until these are fixed.
badstring program will not run because it contains syntax errors.
Can you find them all?
The syntax highlighting is indicating that something is wrong. The strings should be the only text highligted in pink. But this is not the case. Pink is also being used to highlight the function’s names, which should be black.
There are nine syntax errors in the
badstring program. The errors are
PRINTLNis spelled incorrectly. It should be
FMTis spelled incorrectly. It should be
But what would Go do if we tried to run the
badstring without correcting the
errors? Lets find out.
First open your Terminal program or Command Prompt. In your terminal you need to change to the location of your Go Workspace. To do this you need to type
Now you need to make a new directory. We need to call this
the program we will write. Then we need to change directory into the new
mkdir badstring cd badstring
Now you need to start your editor, either Atom or LiteIDE
Once you have typed the program in, you need to save it. Once you have saved it you need to run it with:
go run badstring.go
When you attempt to run the program you should see this.
You read the lines like this
filename:line number:error message
So the filename is the
./badstring.go in this case. The filename includes the
path, in this case the
If you look at the error messages it correctly tells you that line 9 has a syntax error. However if you look at the error message it does not make the error immediately apparent. Neither is the solution apparent.
Also notice that the errors snowball. There errors on line 9, 11, 13, 15, 16 and 20 are found, but the ones on lines 12, 14, and 21 are masked by the earlier errors.
This output is typical of a program that contains syntax errors.
First don’t panic! You can learn how to use this output to help you find the errors. You just need to take three steps.
The first step is to learn the patterns. The error messages tell you where you might have an error. We say might because this where Go detected the error. The cause may be several lines before.
The second part is to be systematic and logical. The computer is not wrong, there is a reason for the error somewhere in the program. Your task is to find it and correct the mistake. You need to look very carefully at the line. A single character added, missed or a letter in the wrong case is sufficient to trigger a syntax error. You use the patterns to find these mistakes.
The last part is to work in stages. You should try to solve one error at a time. Start at the fist error and attempt to fix that. When you make a change that you think fixes the mistake, save the program, and try to run the program again. If you have fixed the error then there should be one less the number of errors in the output. You know the error has been fixed. Then you have to tackle the next error using the same approach.
This is the corrected
The output of the program is
Lightning McQueen: I'm a very famous race car! Luigi: You are famous race car? A real race car? Lightning McQueen: Yes, I'm a real race car, what do you think? Look at me. Luigi: I have followed racing my entire life, my whole life Lightning McQueen: Then you know who I am. I am Lightning McQueen. Luigi: Lightning McQueen! Lightning McQueen: Yes! Yes! Luigi: I must scream it to the world, my excitement from the top of someplace very high. Do you know many Ferraris? Lightning McQueen: No, no, no, they race on the European circuit; I'm in the Piston cup. Lightning McQueen: What? Luigi: Luigi follow only the Ferraris.
Now you know enough about strings to make your own changes. Are you ready for your challenge?
This is the output we want you to add to the end of the
badstring program. Start with the corrected version of the
badstring program and edit to so that it prints this at the end.
Lightning McQueen: Okay, here we go. Focus. Speed. I am speed. One winner, forty-two losers. I eat losers for breakfast. Breakfast? Maybe I should have had breakfast? Brekkie could be good for me. No, no, no, focus. Speed. Faster than fast, quicker than quick. I am Lightning.
That’s it! We’re not going to show you the solution you have to work it out for yourself!