Learn to Code in 5 Minutes a Day: Lesson 4
By Code with ADHD
- 2 minutes read - 380 wordsMy experience with ADHD has been that slow consistent progress is how I achieve lasting results. When I hyperfocus on a new interest, I tend to burn out after a month or two and never touch it again. So this course is an attempt to go back and document how I wished someone had taught me to program. It literally took me years of trying before I felt comfortable programming professionally. At the end of this course, you should be in a much better position than I was when I started programming.
In the last session I talked about unit testing. If you haven’t done that exercise yet, don’t read ahead for the answer, go back and do that.
Session 3 Solution:
hello_test.go
package main
import (
"testing"
)
func TestHello(t *testing.T) {
result := hello("inigo montoya")
expected := "hello, inigo montoya"
if result != expected {
t.Errorf("result was incorrect, got: %v, want: %v.", result, expected)
}
}
hello.go
package main
import "fmt"
func main() {
fmt.Println("hello world")
}
func hello(name string) string {
greeting := "hello, " + name
return greeting
}
If you didn’t get something that works like the above, go back and try again until you do.
Today’s Lesson: More Unit Tests
If you got it working, this lesson will build on that. I want you to go into the hello_test.go code and write 2 more tests:
- Test that if you use the name “Dua Lipa”, calling the hello() function returns “hello, Dua Lipa”. (And for fun, see what happens when you misspell the name. Do you get an error? Do you understand the error?)
- Test that if you use the name “Darth Vader”, calling the hello(name string) function does not return “Luke, I am your Father.”
For the first test it will be mostly cut and paste of the test you already have.
For the second test you will have to figure out how to do a negative comparison. You might try googling something like “comparisons golang”.
Again, I could just give you the answer, but 99% of programming is actually figuring things out on your own. If you want a job where someone just tells you what to do, you can go work in a beer factory putting labels on the bottles.