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

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

Which function is passed here?

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).

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

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

This is very interesting to me

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

Both. Go is fascinating to me

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

Thanks for the information

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)

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)
}
1 Like

Thanks. You have been very nice and very helpful lately

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.

2 Likes

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

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.

1 Like

I’m new here, less than a year

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.

1 Like

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)

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