Learn to Code in 5 Minutes a Day: Lesson 3
By Code with ADHD
- 4 minutes read - 802 wordsYesterday’s solution:
package main
import "fmt"
func main() {
fmt.Println("hello world")
}
When you run it you should be able to see it work like this:
go run
hello, world
If you weren’t able to get this going on your own, go back until you can create this from memory and understand the pieces.
Hint: you may have to run a command like this if you’re getting errors about go.mod:
go mod init hello
Just like it’s possible to do math by counting on your fingers, if you don’t know that 7x7 is 49 without using your fingers you’re going to have a hard time with any higher level math.
It’s fine to go back over and over and play with different options. No one is watching how fast or slow you’re learning this.
I’m also going to avoid explaining too much about the individual pieces and instead focus on having working code. You can google things like “what does a golang package do” or “what does import mean in golang” if you’re interested in understanding the pieces.
Today’s Goal: Unit Testing
Today we’re going to spend time on unit testing. One of my big frustrations when I was learning to program is that as my programs got bigger, I had to keep more and more of their functions in my head. One small error in one place meant a lot of work to figure out where the error was and how to fix it.
Unit testing lets us reach into our programs and validate that individual pieces work. This helps me focus on only one thing at a time and not get overwhelmed by all the pieces I’m juggling.
So today I want you to write a function that takes in a string called “name” and returns “hello, name”.
So if you give it the name “inigo”, it should return the string “hello, inigo”.
I’m going to give you the unit test to validate this, and it will be up to you to piece together how to make the unit test pass. Again, you’ll have to google some things on your own, but that’s 99% of programming. There are no detailed step by step instructions in a real programming job, so, what we are about to do is, in my opinion, the most critical skill to learn.
You are going to need two files:
hello.go
hello_test.go
hello.go should be the same file you already created yesterday. You’ll need to create a new file called hello_test.go.
Inside hello_test.go you should cut and paste this code
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)
}
}
Make sure to save the file.
We run this test by typing:
go test
It should give you an error. What do you think this error means? How do you think you solve this error? Again, reading error messages and figuring out what to do with them is 99% of programming. A good tip if you don’t understand an error message is to google the exact error message. See if someone else has run into it and has an answer for what it means and how to handle it.
Today’s exercise is to figure out how to run “go test” and not get any errors.
- Don’t change any of hello_test.go.
- Only change the code in hello.go to make the test pass.
Hint: the test is testing a function called “hello”, so you’ll need to create a function in hello.go that starts with something like:
func hello(name string) string {
But you’re going to need to figure out the rest of it. Look at how the main() function was made. See if you can replicate that.
You’ll also need your function to end with a line like:
return greeting
}
Finally, you’ll need to write a line between the first bit I gave you and the last bit I gave you that creates a variable called “greeting” and assigns it the correct value. Googling “how to assign variables in golang” might be a good place to look.
Again, yes I could just give you the answer. But if you don’t build the research and problem solving skills early, you’ll be completely lost on problem solving later. This is the number one issue I see with new developers (and even old developers). They memorized steps and trivia but they never learned to come up with their own answers.
Good luck! No time limits. Come back for the next lesson (and the answer) when you figure it out. Whether that’s a day or a week or a month, just keep putting in 5 minutes a day playing with it until you get it.