Method Sets...https://play.golang.org/p/2ZU0QX12a8

During a class about method sets, Todd shared this code. * https://play.golang.org/p/2ZU0QX12a8

What is math.Pi?

He continues with the following code:
https://play.golang.org/p/N0wsEV9N6JA

What is the purpose of “.” in s.area?

Todd had given this information previously: Structs allow us to compose together values of different types.

In: type circle struct {
radius float64
}, I don’t see different types. So why is it a struct? I looked at the Language Spec. and saw that float64 is a type. Is radius a type?

What is “{5}” in “circle{5}”?

What is “&c” pointing to?

1 Like

A constant. math package - math - Go Packages

You can learn more about the meaning of that number at Pi - Wikipedia

Qualification. As usual…

Yes, its a struct with a single field. This allows to not accept a random number where we explicitely want to have circles.

No, its the name of the field in the struct.

5 is a numeric literal. circle{…} is syntax for struct construction. So circle{5} will construct a circle struct with the first field set to 5.

Personally I do not like to use positional construction and always write out field names, eg. circle{ radius: 5}.

1 Like

I figured that , put I didn’t know where ti fid it in the Language Spec.

Qualification. As usual…

I don’t understand.

Yes, its a struct with a single field. This allows to not accept a random number where we explicitely want to have circles.

Interesting.

No, its the name of the field in the struct.

Field?

5 is a numeric literal. circle{…} is syntax for struct construction. So circle{5} will construct a circle struct with the first field set to 5 .

Personally I do not like to use positional construction and always write out field names, eg. circle{ radius: 5} .

Thanks

1 Like

The . always qualifies something in another thing. Its therefore often called the qualification operator. Others may say its the fieldaccess operator or method call operator, but both names are specialisiations of the former and only valid in a certain context.

Structs compose many types into a single one.

Just think of it as a form.

struct foo {
  bar string
}

This creates a “form” with a single “field” you can write things into it. That field is “labeled” bar. You can write anything in that field.

When we change this to be like this:

struct foo {
  bar  string
  quux float64
}

The we get a “form” with two fields, one which can hold any kind of text, the other “field” is only able to have floating point numbers as its input.

2 Likes

Thanks

1 Like

What is the definition of qualifies, qualification here? I can’t find it by google

1 Like

Have a look at “A Tour of Go”
https://tour.golang.org/moretypes/2

It will give you a better understanding of what structs are and how to use them.

2 Likes

Thanks! It’s been awhile since I looked at the tour, so I’ll do it now…
Fascinating! I’m sure I’ll have to come back and stud it again.

1 Like

is 78.53981633974483 a float64?

In “func info(s shape)”, is there a receiver? Am I right in thinking that info is the identifier of the function?

In https://play.golang.org/p/N0wsEV9N6JA, why doesn’t “&c” yield the address of c

Similar question in https://tour.golang.org/moretypes/4. Why doesn’t “&v” yield an address? That was my understanding of a pointer.

1 Like

It’s an untyped floating point literal. Its final type will be inferred from the context.

No, there is no reciever. In a function definition, the receiver is a single “argument” specified between func and the functions identifier.

But yes, info is the functions identifier, or also its name. At least under the assumptions that this is taken from a function definition.

It is the address of c, passed into a function that expects a certain interface. Since cs type does implement this interface, go does acceppt and will automatically dereference where necessary.

It does, but its still tagged by a type and the compiler can make sense of the combination “address with type”.

2 Likes

I attempted to edit my question above yesterday, but I never pushed the button

1 Like

I edited my post as well to cover all answers, forgot to mention it yesterday…

2 Likes

Thanks so much! These answers really help,

1 Like

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.