Structs in Go (challenge answer)
Tags: beginners, tutorial, learning, go
Published: 2019-08-21 13:31:32

---
title: Structs in Go (challenge answer)
published: true
tags: beginners, tutorial, learning, go
canonical_url: https://www.codetips.co.uk/languages/go/structs-in-go-challenge-answer/
---

 ![Structs in Go (challenge answer)](https://images.unsplash.com/photo-1432821596592-e2c18b78144f?ixlib=rb-1.2.1&q=80&fm=jpg&crop=entropy&cs=tinysrgb&w=1080&fit=max&ixid=eyJhcHBfaWQiOjExNzczfQ)

Warning: **Challenge spoiler alert**

At the end of the “[Structs in Go](https://www.codetips.co.uk/structs-in-go/)” article, we asked you to complete the following tasks:

1. Make a new field in our `person` struct, of type `bool`, that we’ll use to control whether someone is a friend or not.
2. Use the new field to control which statement is printed out.

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.

The first task is to add a new field in our `person` struct, that we can use to control whether someone is a friend or not. We’ll call this new field `isFriend`, to remain consistent.

<!--kg-card-begin: code-->
```
type person struct {
	name string
	age int
	isFriend bool
}

```
<!--kg-card-end: code-->

But we’ve now got a `too few values in composite literal` error, where we’re using the `person` struct (below).

<!--kg-card-begin: code-->
```
me := person{"Simon", 29}

friends := [5]person{
	{"David", 17},
	{"Bill", 42},
	{"Charlie", 12},
	{"Abby", 24},
	{"Edith", 74},
}

```
<!--kg-card-end: code-->

This error basically means we’re not passing enough values through; our `person` struct expects three properties (`name`, `age` and `isFriend` ) but we are only passing through two ( `name` and `age` )

Let’s fix this by specifying the `isFriend` property.

<!--kg-card-begin: code-->
```
me := person{"Simon", 29, false}

friends := [5]person{
	{"David", 17, true},
	{"Bill", 42, true},
	{"Charlie", 12, true},
	{"Abby", 24, true},
	{"Edith", 74, true},
}

```
<!--kg-card-end: code-->

That’s the first task done so lets now update our programme to use the new `isFriend` field to control which statement is printed out.

We’re already passing the variable, of type `person`, through to the `printAgeBracket` function, so we already have access to the `isFriend` property.

The only change we need to make, in the `printAgeBracket` function, is to use the new `isFriend` field (`p.isFriend`) instead of the `isFriend` parameter.

<!--kg-card-begin: code-->
```
func printAgeBracket(p person, isFriend bool) {
	if p.isFriend { // Change this line
		log.Printf("%s: ", p.name)
	} else {
		log.Printf("Hello World. My name is %s.", p.name)
	}

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

```
<!--kg-card-end: code-->

And now your programme is using the new `isFriend` field! So, we’re done? Not just yet.

Our programme works fine, but we’ve left our `isFriend` parameter even though we’re not using it. It’s good practice to clean up unused code; it will help prevent bugs in the future and makes the code look cleaner.

To remove the `isFriend` parameter from the `printAgeBracket` function, we just remove it from within the function parenthesis:

<!--kg-card-begin: code-->
```
func printAgeBracket(p person) {
	if p.isFriend {
		log.Printf("%s: ", p.name)
	} else {
		log.Printf("Hello World. My name is %s.", p.name)
	}

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

```
<!--kg-card-end: code-->

And then update anything that calls it, to avoid any errors:

<!--kg-card-begin: code-->
```
printAgeBracket(me)

for i := 0; i < len(friends); i++ {
	printAgeBracket(friends[i])
}

```
<!--kg-card-end: code-->

Remember you can always [reach out to us](https://www.codetips.co.uk/contact-us/) with any questions or feedback - we’d love to hear from you!

---

[CodeTips](https://www.codetips.co.uk) 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](https://mailchi.mp/92cccf7024b2/codetips) and get the newest articles straight to your mailbox? 

The original source for this content is [CodeTips](https://www.codetips.co.uk). The original content is kept up-to-date, but other sources may not be the latest version.