I’m still around.
In this hands-on exercise, I was instructed to " Create a func which returns a func".
So I created this:
So far so good.
Next, I was instructed to " assign the returned func to a variable".
Here is my attempt.
When i formatted it, I got this message “prog.go:11:2: expected declaration, found sweettime”
Isn’t this a declaration? “sweettime := func()”
I don’t know which part of this topic should be surrounded by ```

I think I got it!

Any comments?

Hi Cherolyn

I’m happy to see you are still learning Go. :slight_smile:

Your final program looks good.

About your attempt that didn’t work, maybe you were trying to do something like this:

or this

or this

There are many ways to use functions. I just wanted to show you some variations. Actually, the way you did it is very common and many people prefer it.


Thank you very much! I like all three of your solutions!


Can someone explain to me what all these parentheses are for? I think I know, but I would like it to be stated.

The function Hawaii is called by using Hawaii() and it returns a function. This returned function is called with the second pair of parentheses. The return of that function is then printed with fmt.Println(). One could have written it something like this instead

someFunction := Hawaii()
result := someFunction()

Your answer is interesting, informative, and helpful. Thanks


func sweettime() int {
	fmt.Println("Amazing time")
	return 1981

Here sweettime both prints “Amazing time” and also returns an int? int is a return isn’t it? Yet it also can print something?

Exactly! fmt.Println is going to print whatever you give it. Regardless of where you call it, whether it be in the body of a function or not.

func sweettime() int {
	fmt.Println("Amazing time")
	return 1981

Your sweettime function takes no parameters and returns an int. The return type is specified after the parentheses, but before the opening curly bracket.

If you were to remove return 1981 you would get a error, becuase by providing a return type you’re telling Go that it should expect an integer to be returned whenever sweettime is called.

You could alter your sweettime function to return a string of text as opposed to an integer like this:

func sweettime() string {
	fmt.Println("Amazing time")
	return "Sweet Time!"

and again, fmt.Println would still print "Amazing time" regardless, but this time rather than Go expecting an integer to be returned, it’ll expect a string which in the above case is "Sweet Time!".

An example of how you would use these returned values maybe a function that returns your name as a string.

func getMyName() string {
    return "Cherolyn Lexvold"

You could then define a variable equal to the returned value of calling that function. Like this:

func main() {
    myName := getMyName() // myName is now equal to "Cherolyn Lexvold"

   // now you have your name as a variable you can print it using fmt.Println

Here’s an example of the above that you can run:

The official Golang language tour has a section on Functions that you might find helpful

I hope this was helpful!

Like this?

Not only was your reply helpful, it was also thoroughly enjoyable!

fmt.Println is a very cool feature!

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