Structs, Hands-on exercise

This is what I got,

Here is the teacher’s solution:

It appears to me that the error messages required me to have one more } at the end than his did. Anyone know the reason?

Press the blue button named format and you will see this you put a couple of extra curly brackets around the fmt.Println statement.


The next hands-on exercise continued from that code. I am here:

Without going forward, because I want to continue the exercise myself, explain to me in “last”:p1. the purpose of the dot in p1.

It accesses the fields or methods in p1.

Trying not to feel stupid, I don’t know what you mean by fields and methods

fields are components of your struct and methods are functions applied to a object.

Don’t feel stupid. It’s new to you. Think of a strict as a thing. That thing has characteristics, properties, and that thing also has behaviors which are functions.

That’s ok if you don’t know… I guess we’re all here to share knowledge and learn with each other.

I have no idea why I did that


fields are components of your struct and methods are functions applied to a object.

I’m going to save this.

I will also save this.

It accesses the fields or methods in p1 .

This is helpful and now orients me for the replies that follow.

Thanks for the encouragement :slight_smile:

Thanks :slight_smile:

Continuing the exercise, what is wrong with this:

This is a previous attempt.

You forgot the closing brace for p2 := person{ .... And also you needed to declare and set the value of m before trying to print it.

I tried, and got

After hitting format you see this:

	m := map[string]person{
		p1.last: p1,
		p2.last: p2,

So the compiler basically sees something equivalent to this: m := 1, 2.

He tells you this in the error message as well:

prog.go:24:4: assignment mismatch: 1 variables but 2 values

The two remaining error messages are related to this, and can be ignored in this case.

Please, when you have code that does not work, post the error or unexpected output as well and try to explain what you had expected instead, or what you do not understand with the error message.

Sooner or later you really need to understand them in your own, or you will never be able to do something beyond a hello world.