Callback exercise...trying again https://play.golang.org/p/7AH5xa2w-2c

What exactly are you trying to achieve in this line?
fmt.Println(incomeforthenextfouryears(func() yearlyincrease, int)(int)int{

1 Like

Good question, right? lol
Well, the ultimate goal is to “pass a func into a func as an argument”.
So I thought I would print each year from 2019-2024, and for each year show an increase of 1,000. So I attempted to make a function that would calculate the increase, and then call that function when I listed each year. Could I use a callback to accomplish this even?

Of course. But let’s back to your goal - pass func to another func as argument.

  1. Define type of our argument, for example so:
    type printInt func(int)
    Because function it’s also just a type.

  2. Write function with argument of printInt type and use it inside

     func test(arg printInt) {
         for i := 0; i < 5; i++ {
     	    arg(i)
         }
     }
    
  3. Create a variable of printInt type(like any other type):

     var f printInt = func(p int) {
     	    fmt.Println(p)
         }
    
  4. Call it test(f)

And that’s all. Full example - https://play.golang.org/p/Hkf0Da2xO-B

What is the purpose of defining the type?
How does this look?
type yearlyincrease func(int)
Tried to apply

func test(arg printInt) {
     for i := 0; i < 5; i++ {
 	    arg(i)
     }
 }

to my program. Got “prog.go:14:6: expected ‘(’, found test (and 2 more errors)”
Check out or ignore and keep going with what you’re doing:

https://play.golang.org/p/mpj3l9vES1M

For simplification [understanding if you are beginner] or readability [if you more experienced].

Not bad, just a few advices:

  1. Don’t change everything at one time. One change - one success.
  2. Read what the tool is telling you.

Fixed : https://play.golang.org/p/PHODb5FUY-B

Cool.

From the Language Specification.

A type determines a set of values together with operations and methods specific to those values. A type may be denoted by a type name , if it has one, or specified using a type literal , which composes a type from existing types.

Type = TypeName | TypeLit | “(” Type “)” . TypeName = identifier | QualifiedIdent . TypeLit = ArrayType | StructType | PointerType | FunctionType | InterfaceType | SliceType | MapType | ChannelType .

type printInt func(int)

So “printInt” is the TypeName? and “func(int)” is the type?

Right.

1 Like

Thanks :pray:. More tomorrow. Out of time :confused:

I don’t know what you’re referring to here.

Read what the tool is telling you.

What tool? I don’t know what a tool is.

Well … writing every program starting with some template. And if you write code incrementally and not copy-paste you can’t get many compile error at one time. Right?
Compile time error is basic level , for fixing it should be enough only main pages from documentation/ examples from the package / … quick google search at the end.

a compiler, a linter, an application.

1 Like

It seems answers to questions lead to more questions. I will keep at it. Out of time. Thanks for your patient help.

Bear with me.
Your code https://play.golang.org/p/PHODb5FUY-B
My code https://play.golang.org/p/ZegcfjAzHT5
Ignore my comments .
Why am I getting this error message when I format? “prog.go:11:6: expected ‘(’, found test (and 1 more errors)”.
By the way, what does 11:6 mean. I get the 11 part; I’m confused by the 6

You can not create a namend function inside another function. You have to create an anonymous function and assign it to a variable. That is why you get that error.

file.go:11:6 means, in the file file.go at line 11, the 6th column. Be aware though, that indenting with a TAB counts as a single column per tab.

Nice to hear from you again.

So is “incomeforthenextfouryears” the named function that you’re talking about? Isn’t that a named function that the gentleman used? “printInt”

Which gentleman used printInt where?

The only reference to printInt I’m able to find on a quick glance is in Callback exercise...trying again https://play.golang.org/p/7AH5xa2w-2c, and there it is a type name.

A type name is neither a named or anonymous function. Its just a type, types though can be used to describe named or unnamed functions if they are of func-“kind”.

https://play.golang.org/p/Hkf0Da2xO-B

Here, from GreyShatter. He uses “printInt” int apparently the same place that I use “incomeforthe nextfouryears” in https://play.golang.org/p/inVJYZc93XD
Yet I get an error message: “prog.go:11:6: expected ‘(’, found test (and 1 more errors)”
What did I miss?

No, he doesn’t. He does use it as a type.

Did I not use “incomeforthe nextfouryears” as a type in the same fashion?

Let me start again. The objective is a hands-on exercise with a callback…" * pass a func into a func as an argument".
So my idea was to make a function that listed several years, 2019-2023, and to pass in a function that would calculate the yearly increase (my idea was start with 40,000 and increase by 1000 each year). Does that even make sense to try?

Of course. But you have all that you need. If on these step something not clear better do one step back and do it again. If task don’t go forward - split it. Forget about callback, write your function separately.