Structs, Hands-on exercise

This is what I got, https://play.golang.org/p/_NsL947SE9a

Here is the teacher’s solution: https://play.golang.org/p/2mgWOEh0CYa

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 https://play.golang.org/p/xILFPwu0WW1 you put a couple of extra curly brackets around the fmt.Println statement.

Thanks

The next hands-on exercise continued from that code. I am here: https://play.golang.org/p/ubUm3l1PcOB

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.
Regards,

I have no idea why I did that

Cool

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: https://play.golang.org/p/vQVx2pdivUF

This is a previous attempt.

https://play.golang.org/p/7vn5j-wZmEz

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.

https://play.golang.org/p/OEEZBk3p3uE

I tried, and got https://play.golang.org/p/_EYihbBNPkM

After hitting format you see this:

	m := map[string]person{
		p1.last: p1,
		p2.last: p2,
	},
		fmt.Println(m)

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.