Functions and receivers func ( r receiver)


(Cherolyn Lexvold) #1

In “func ( r receiver)”
what is “r”? Is it an identifier?
Sorry if my questions sound infantile. I have nowhere else to ask such questions


(Norbert Melzer) #2

https://tour.golang.org/methods/1

In this example, the Abs method has a receiver of type Vertex named v .

To be honest, what should it be otherwise? even receiver is an identifier… At least in the place where you have that word…


(Cherolyn Lexvold) #3

https://tour.golang.org/methods/1

Go does not have classes. I have no idea what that means.

There are so many things on this page that I know nothing about.

even receiver is an identifier Thanks!

But this is the section you’re referring to: the Abs method has a receiver of type Vertex named v .

Thank you. I just have to keep at it and eventually it will become clear


(Cherolyn Lexvold) #4

So this receiver is named r?


(Cherolyn Lexvold) #5

Just working on terminology here


(Norbert Melzer) #6

Yes. But even though its of type receiver, its not necessarily the receiver of a method, as the snippet you have shown is either incomplete (it lacks the actual method name, arguments and return type) or is the type of a function that takes a single argument of type receiver and returns nothing/void


(Cherolyn Lexvold) #7

To be more specific, when Go speaks of an identifier, is this a term that is specific to Go? And/or programming? Or are we to understand “identifier” as the common English definition “a person or thing that identifies something.” In looking for this last, I found, "COMPUTING a sequence of characters used to identify or refer to a program or an element, such as a variable or a set of data, within it.
I must be on the right track.
While possibly seeming elementary, this word has sounded like a foreign language to me, as I consider all the elements of Go until I know their definition specifically.


(Cherolyn Lexvold) #8

This is useful


(Cherolyn Lexvold) #9

More terms that almost, but not quite, mystify me.

Method, I have no clue.

Name, I’ve got down.

Arguments, I’m starting to get, but I don’t have a clear definition.

Return, all I’ve got in my head is that it’s something that returns something…so a bit nebulous.

Type used to really confuse me. Previously type to me was something that you typed, ie with a typewriter (I’m old) or computer keyboard. This definition has been very helpful to me: “a classification of data”


(Lutz Horn) #10

May I point you back to this advice?

I fear that for you Go and this forum are not good choices for starting to learn programming.

A completely different approach could be Scheme and the very good and thorough book How to Design Programs (available online). Please take a look, not to short, at this book. Spend some hours or days trying to understand what it teaches. Perhaps this is an approach that works for you.


(Cherolyn Lexvold) #11

Thank you for your opinion, but I don’t think you are correct


(Gonzalo Chumillas) #12

I understand it as “the target parameter”.


(Raul Gauna) #13

i was taking notes while taking an online tutorial:

import “fmt” /*grabs the “format” module from the package main…you can use
fmt.Println with this. you can add an alias to “fmt” ex: import f “fmt”
this will allow you to use f.Println() instead of fmt.Println(). Can be useful
for long named imports. */

so, based on this, I believe you’re giving ‘receiver’ the alias r, so that when you call on it later, you can just type r instead of having to spell out receiver. that’s my guess anyways…someone correct me if i’m wrong 'cause I’m still learning. :stuck_out_tongue:


(Lutz Horn) #14

What you describe is not the receiver of a method. It is an alias for an import.

package main

import (
	f "fmt"
)

func main() {
	f.Println("Hello, playground")
}

Here f is an alias for fmt. This can be useful to avoid name clashes.

To learn about what a receiver is, please read the documentation. A good starting point is the Go Tour. In addition, the book The Go Programming Language is still a very good guide to learn Go.

Wild guesses and hearsay are not recommended to learn anything.


(Cherolyn Lexvold) #15

Ah! I was looking for this: A method is a function with a special receiver argument.

By the way, I discovered I am on page 17 of the second module? or lesson of the tour. I have also made it a Chrome favorite three times. I’ve resolved to make my way through the tour as time allows.

Your and others specific references to the tour are very helpful to me.

Does Vertex have special significance in Go?


(Norbert Melzer) #16

No, it is just a name of a type in that particular example.


(Cherolyn Lexvold) #17

Thankfully I have important people in my life who would disagree with you and several others who concur with you. One is my sister, who of course knows me intimately and has the opinion that I will succeed at anything I put my mind to. By the way, my sister is a successful systems engineer who previously dropped out of high school. She thinks I am smarter than her.
The second person is my teacher who believes, all things considered, that I have made great progress. I choose to follow the guidance of these two important people rather than you guys who would discourage me…to what purpose I can’ figure out. Why should you care? Isn’t that my business?
On this, “within a reasonable time”, I have my whole life! If I should die or become senile before I become proficient in Go, I still feel I will have benefitted from it’s study. So please, back off! :wink: (Said lovingly)


(Cherolyn Lexvold) #18

“the target parameter”

Very interesting. Please expand.


(Cherolyn Lexvold) #19

import “fmt” /*grabs the “format” module from the package main…you can use
fmt.Println with this. you can add an alias to “fmt” ex: import f “fmt”
this will allow you to use f.Println() instead of fmt.Println(). Can be useful
for long named imports. */

This is interesting, but a little beyond me
whereas this "so, based on this, I believe you’re giving ‘receiver’ the alias r, so that when you call on it later, you can just type r instead of having to spell out receiver. " is VERY helpful


(Norbert Melzer) #20

But not what actually happens!

r is not an alias of receiver! r is of type receiver in the example you have shown. Function/Method declarations are semantically and syntactically distinct from the import statements.