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


( Kvaz1r) #2

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


(Cherolyn Lexvold) #3

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?


( Kvaz1r) #4

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


(Cherolyn Lexvold) #5

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


( Kvaz1r) #6

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


(Cherolyn Lexvold) #7

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?


( Kvaz1r) #8

Right.


(Cherolyn Lexvold) #9

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


(Cherolyn Lexvold) #10

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.


( Kvaz1r) #11

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.


(Cherolyn Lexvold) #12

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


(Cherolyn Lexvold) #13

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


(Norbert Melzer) #14

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.


(Cherolyn Lexvold) #15

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”


(Norbert Melzer) #16

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


(Cherolyn Lexvold) #17

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?


(Norbert Melzer) #18

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


(Cherolyn Lexvold) #19

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


(Cherolyn Lexvold) #20

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?


( Kvaz1r) #21

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.