0

I'm trying to create a func that accepts and returns any number of arguments. I came across anoymous functions:

  func AWSApiRetry(awsFunc func()) {
        return awsFunc()
    }

This allows me to call a func:

AWSApiRetry(func() {
                    GetEnvState(sess, ApplicationName, EnvName)
                })

but when I try to retrieve the return values from GetEnvState which are (string, err):

ElbReady, err := AWSApiRetry(func() {
                GetEnvState(sess, ApplicationName, EnvName)
            })

I'm getting the error: AWSApiRetry(func literal) used as value

How can I use my AwsApiretry func to return those types anonymously. It can be any number and type of return values, so it just sort of 'pass-through' and returns whatever the func being called returns.

2
  • 1
    Go does not have that feature. What is the higher-level problem that you are trying to solve? Perhaps there's a solution to that problem. Commented Feb 29, 2020 at 1:19
  • 1. @buildmaestro, you're defining a function that has no return, but then returning the value returned by the passed function. Does it return or not? 2. You can actually create functions of arbitrary signature using the reflect package. This isn't truly a variadic in/out, but it'd get pretty close. If you still care about this example, I'd love to see more detail about what you were trying to accomplish, and maybe I could write a real answer. Commented Jun 28, 2021 at 3:40

3 Answers 3

1

You cannot.

AFAIK, in its current form, go is statically typed. What you want to do is create a function/method that returns types not known during compile time. go, by design, does not allow you to create a function/method like that

Sign up to request clarification or add additional context in comments.

Comments

0

This question is roughly a year old, but no one has correctly identified the problem: the AWSApiRetry() function signature has no return. You cannot assign the return value of a function having no return value.

The actual API you're aiming to offer, defining a function with an arbitrary signature, can be accomplished using the function MakeFunc(), in the standard reflect package.

If you wanted to get more specific about the needs you observed, I'd be happy to propose a specific solution.

Comments

0

Does this help answer part of your question?

package main

import "fmt"

func main() {
  GetEnvState := func(i ...int) {
     fmt.Println(i)
  }
  AwsApiretry := func(awsFunc func()) {
     awsFunc()
  }
  AwsApiretry(func() { GetEnvState(1) }) // prt [1]
  AwsApiretry(func() { GetEnvState(1, 2) }) // prt [1 2]
  //ElbReady := AwsApiretry(func() { GetEnvState(1, 2, 3) })
  //fmt.Println(ElbReady) // AwsApiretry(func literal) used as value
}

But I also agree with @Cerise and @Kelsnare terrific answers. It is not possible to call a function/method that returns an unknown type. When ElbReady is added you get an error:

AwsApiretry(func literal) used as value

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.