If

Reading material

Go by example: If/Else

A Tour of Go: If and Else,
If, If with a short statement


First exercise


// if1
// Make me compile!

// I AM NOT DONE
package main

import "testing"

func bigger(a int, b int) int {
	// Complete this function to return the bigger number
	// Use only if statements
	if a > b {
		return a
	} else {
		return b
	}
}

func TestTwoIsBiggerThanOne(t *testing.T) {
	if bigger(2, 1) != 2 {
		t.Errorf("2 is bigger than 1")
	}
}

func TestTenIsBiggerThanFive(t *testing.T) {
	if bigger(5, 10) != 10 {
		t.Errorf("10 is bigger than 5")
	}
}

Second exercise

// if2
// Make me compile!

// I AM NOT DONE
package main

import "testing"

func fooIfFizz(fizzish string) string {
	if fizzish == "fizz" {
		return "foo"
	} else {
		return "complete me"
	}
}

func TestFooForFizz(t *testing.T) {
	result := fooIfFizz("fizz")
	if result != "foo" {
		t.Errorf("should be 'foo' but got %s", result)
	}
}

func TestBarForFuzz(t *testing.T) {
	result := fooIfFizz("fuzz")
	if result != "bar" {
		t.Errorf("should be 'bar' but got %s", result)
	}
}

func TestDefaultForBazz(t *testing.T) {
	result := fooIfFizz("random stuff")
	if result != "baz" {
		t.Errorf("should be 'baz' but got %s", result)
	}
}


If something is wrong in this page, edit this page on GitHub