Learn to Code in 5 Minutes a Day: Lesson 6
By Code with ADHD
- 3 minutes read - 428 wordsRecap of progress to date
So far you’ve learned some basics of writing code. We haven’t talked about terms, but you’ve learned how to compile and format code, how to create functions, how to define variables, how to unit test, and how to do conditional statements (aka if statements). Even more importantly, you’ve learned how to search for answers and experiment. You’ve got a lot of the building blocks for coding. If you’re feeling shaky about any of the lessons so far, go back and repeat them. It is absolutely OK to revisit over and over until you feel good about them. Spaced repetition is actually a well-tested very reliable method for learning new things, especially languages.
Think about learning a foreign (human) language: did you have to repeat words and concepts over time? Of course you did.
The same concept applies to learning computer languages. So if you feel like you don’t have a good grasp on what we have done so far,go back and do it over from scratch as many times as you need to. There’s no deadline and no judgement.
Today’s lesson: your first Roman Numeral
Let’s create two new files:
- roman.go
- roman_test.go
Can you figure out how to create them and add the boilerplate text at the top of them? No need to memorize this sort of thing, you can look at how hello.go and hello_test.go were created and copy from there.
In roman.go we will create a function like so:
roman.go
func roman(number int) string {
return ""
}
in roman_test.go we will create a unit test for it:
roman_test.go
func TestRoman(t *testing.T) {
result := roman(1)
expected := "I"
if result != expected {
t.Errorf("result was incorrect, got: %v, want: %v.", result, expected)
}
}
Run your test and make sure it fails. As dumb as it sounds, this is key to keeping you on track. Always make sure your test fails first. Then add the code that makes it pass.
go test
--- FAIL: TestRoman (0.00s)
roman_test.go:11: result was incorrect, got: , want: I.
FAIL
exit status 1
FAIL hello 0.240s
Is this what you see? If you get other errors, can you fix the other errors? Remember that dealing with unforeseen errors is 90% of the hard part of programming and it’s totally normal.
If this is the error you see, can you figure out how to update the roman(number int) function to make this test pass and the error go away?
Play around with that and next session we will work on the next roman numerals.