Passing a function https://play.golang.org/p/6QAnd--O_hP


(Cherolyn Lexvold) #1

https://play.golang.org/p/6QAnd--O_hP

Which function is passed here?


(Norbert Melzer) #2

In this line:

	t := evenSum(sum, []int{1, 2, 3, 4, 5, 6, 7, 8, 9}...)

You are passing the function sum to the function evenSum.

In evenSum then it is bound to f via the functions argument (f func(x ...int) int makes f accept any function that is variadic and takes only ints).


(Cherolyn Lexvold) #3

Thanks. I think that answers another question that I had, but I didn’t know how to phrase. The question was , in “func sum(x …int) int {”, whether “sum” is a random name for the function , or is it predetermined


(Norbert Melzer) #4

It’s freely choose (according to the rules of allowed identifier) by the developer.


(Cherolyn Lexvold) #5

This is very interesting to me


(Norbert Melzer) #6

What is “this”? Naming stuff or passing around functions?


(Cherolyn Lexvold) #7

Both. Go is fascinating to me


(Norbert Melzer) #8

Both of this is not very different from any modern programming language.


(Cherolyn Lexvold) #9

Thanks for the information


(Cherolyn Lexvold) #10

Studying this post.
In for _, v := range x , does v always mean value? Or is v arbitrary (I seem to ask that question over and over, but I want to make sure it holds true in every instance)


(Norbert Melzer) #11

range returns 1 or 2 values. The first is always the index/key (depending on the type of ranges argument) and the second is the value.

It is a common idiom to use k and v when iterating over a map[…]… type or i and v when iterating over array or slice types.

But as names are part of documenting the code, I also often tend to give meaningful names. Very often just the singular of the containers name (which I usually have in plural form for collection types).

for _, name := range names {
  fmt.Println(name)
}

(Cherolyn Lexvold) #12

Thanks. You have been very nice and very helpful lately


(Norbert Melzer) #13

Believe me, I’ve not changed my mind and I still think you should find someone in your neighborhood teaching you the most basic concepts, that you haven’t understand in the last ~6 months. You are still turning in circles.


(Cherolyn Lexvold) #14

Unfortunately, I know none of my neighbors other than to say hello and I like your dog


(Norbert Melzer) #15

When I say “neighborhood”, I’m not speaking about the houses you see when you open your door. Its more in the metaphorical sense. Neighborhood means everything and everywhere you can reach without loosing too much time or money.

For me this is roughly everywhere between my home and my office in a corridor of 3km width, and therefore an area of about 90 square kilometers.

If I leave my car in a P+R station I can even extend this by another 20 to 30 square kilometers, as some regions are easier to reach by train/busses here than by car.

If though you are living in a technologycally underdeveloped village as I did during my youth, I can somewhat understand that its hard to find some nerds there. But in such villages one usually knows the guy from the other side of the street much better than you describe.


(Cherolyn Lexvold) #16

I’m new here, less than a year


(Norbert Melzer) #17

When I moved to my current place, I was as well. But actually searching for local user groups and meetups really helped to find more people, and finally it even helped to get a foot into my current employers door.


(Cherolyn Lexvold) #18

Interesting.

searching for local user groups and meetups really helped to find more people

I’m embarrassed to say that I forgot how you said to mark such quotes, and I can’t fins the place where you so instructed me.

I would love to do that! My sister introduced me to something called Slack. I downloaded it but have not made use of it as yet. I think it’s possible it might have a similar aim. (Actually, I’m waiting on her)


(system) closed #19

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