DEV Community

Si for CodeTips

Posted on • Originally published at codetips.co.uk on

Functions in Go (challenge answer)

Functions in Go (challenge answer)

Warning: Challenge spoiler alert

At the end of the last article ( “Functions in Go” ) we asked you to update the printAgeBracket function to accept a Boolean parameter, so we could conditionally print a statement and reduce duplication.

If you haven't attempted it; we're about to give you the answer, so now's a good time to go and try it.

For reference, here was the printAgeBracket statement we asked you to amend:

func printAgeBracket(name string, age int) {
    log.Printf("%s: ", name)

    if age < 13 {
        log.Println("I am considered a child")
    } else if age < 20 {
        log.Println("I am considered a teenager")
    } else if age < 70 {
        log.Println("I am considered an adult")
    } else {
        log.Println("I am considered a pensioner")
    }
}
Enter fullscreen mode Exit fullscreen mode

And this was the code we wanted to incorporate into this function:

log.Printf("Hello World. My name is %s.", myName)

if myAge < 13 {
    log.Println("I am considered a child")
} else if myAge < 20 {
    log.Println("I am considered a teenager")
} else if myAge < 70 {
    log.Println("I am considered an adult")
} else {
    log.Println("I am considered a pensioner")
}
Enter fullscreen mode Exit fullscreen mode

The only thing stopping us from using the printAgeBracket function is we print out a different statement; Hello world. My name is: %s vs $s:.

One possible solution to this problem is to add a new parameter to printAgeBracket, called isFriend, and add an if/else statement to our function:

func printAgeBracket(name string, age int, isFriend bool) {
    if isFriend {
        log.Printf("%s: ", name)
    } else {
        log.Printf("Hello World. My name is %s.", name)
    }

    if age < 13 {
        log.Println("I am considered a child")
    } else if age < 20 {
        log.Println("I am considered a teenager")
    } else if age < 70 {
        log.Println("I am considered an adult")
    } else {
        log.Println("I am considered a pensioner")
    }
}
Enter fullscreen mode Exit fullscreen mode

We then need to amend the lines that use this function to use this new parameter:

printAgeBracket(friendOneName, friendOneAge, true)
printAgeBracket(friendTwoName, friendTwoAge, true)
printAgeBracket(friendThreeName, friendThreeAge, true)
printAgeBracket(friendFourName, friendFourAge, true)
printAgeBracket(friendFiveName, friendFiveAge, true)
Enter fullscreen mode Exit fullscreen mode

If our new isFriend parameter is true, we print out the first statement, else we print out the second statement.

If you didn’t get this far on your own, take five minutes and see if you can amend the following code to use the printAgeBracket function:

log.Printf("Hello World. My name is %s.", myName)

if myAge < 13 {
    log.Println("I am considered a child")
} else if myAge < 20 {
    log.Println("I am considered a teenager")
} else if myAge < 70 {
    log.Println("I am considered an adult")
} else {
    log.Println("I am considered a pensioner")
}
Enter fullscreen mode Exit fullscreen mode

Done? Excellent!

You should have ended up with the following:

printAgeBracket(myName, myAge, false)
Enter fullscreen mode Exit fullscreen mode

The isFriend parameter is false so it prints out the statement in the else statement, and we’ve just removed more duplication from our code.

Our whole programme now looks like this:

package main

import "log"

func printAgeBracket(name string, age int, isFriend bool) {
    if isFriend {
        log.Printf("%s: ", name)
    } else {
        log.Printf("Hello World. My name is %s.", name)
    }

    if age < 13 {
        log.Println("I am considered a child")
    } else if age < 20 {
        log.Println("I am considered a teenager")
    } else if age < 70 {
        log.Println("I am considered an adult")
    } else {
        log.Println("I am considered a pensioner")
    }
}

func main() {

    // Start of our code

    myName := "Simon"
    myAge := 29

    friendOneName := "David"
    friendOneAge := 17

    friendTwoName := "Bill"
    friendTwoAge := 42

    friendThreeName := "Charlie"
    friendThreeAge := 12

    friendFourName := "Abby"
    friendFourAge := 24

    friendFiveName := "Edith"
    friendFiveAge := 74

    printAgeBracket(myName, myAge, false)

    log.Println("I have five friends, and they are as follows:")

    printAgeBracket(friendOneName, friendOneAge, true)
    printAgeBracket(friendTwoName, friendTwoAge, true)
    printAgeBracket(friendThreeName, friendThreeAge, true)
    printAgeBracket(friendFourName, friendFourAge, true)
    printAgeBracket(friendFiveName, friendFiveAge, true)

    // End of our code

}
Enter fullscreen mode Exit fullscreen mode

Our new function has reduced a lot of duplication in our code, and it makes maintaining our programme easy (e.g. adding a new age bracket), but there’s still a couple of things that we should improve:

  1. Every time we create a new friend, we have to create two new variables. We’ve currently got fifteen lines of code just to create five friends.
  2. When we print how many friends we have ( “I have five friends, and they are as follows" ), we have to add the number (five) manually. We have a real risk that we could add/remove a friend(s) and forget to update this.

Both of these things have reduced our maintainability. Stay tuned for future articles, where we’ll fix these problems.

If you need any help understanding what we’ve discussed in this article, reach out to us and we’ll do what we can to help.


CodeTips strives to help beginners, with zero or very little experience, learn to code.

We do cross-post to other sites to reach a wider audience, but why not subscribe to our newsletter and get the newest articles straight to your mailbox?

The original source for this content is CodeTips. The original content is kept up-to-date, but other sources may not be the latest version.

Top comments (0)