Structs, Hands-on exercise


(Cherolyn Lexvold) #1

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?


(Johan Dahl) #2

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.


(Cherolyn Lexvold) #3

Thanks


(Cherolyn Lexvold) #4

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.


(Norbert Melzer) #5

It accesses the fields or methods in p1.


(Cherolyn Lexvold) #6

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


(Yamil Bracho) #7

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


(Derek Kenney) #8

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.


(Kevin Maduro) #9

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


(Cherolyn Lexvold) #10

I have no idea why I did that


(Cherolyn Lexvold) #11

Cool

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

I’m going to save this.


(Cherolyn Lexvold) #12

I will also save this.


(Cherolyn Lexvold) #13

It accesses the fields or methods in p1 .

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


(Cherolyn Lexvold) #14

Thanks for the encouragement :slight_smile:


(Cherolyn Lexvold) #15

Thanks :slight_smile:


(Cherolyn Lexvold) #16

Continuing the exercise, what is wrong with this: https://play.golang.org/p/vQVx2pdivUF


(Cherolyn Lexvold) #17

This is a previous attempt.

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


(Jay Ts) #18

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


(Cherolyn Lexvold) #19

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


(Norbert Melzer) #20

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.