Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 5840569
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T11:46:01+00:00 2026-05-22T11:46:01+00:00

I try to write a function which takes any other function and wraps a

  • 0

I try to write a function which takes any other function and wraps a new function around it. This is what I have tried so far:

package main

import (
    "fmt"
)

func protect (unprotected func (...interface{})) (func (...interface{})) {
    return func (args ...interface{}) {
        fmt.Println ("protected");
        unprotected (args...);
    };
}

func main () {
    a := func () {
        fmt.Println ("unprotected");
    };
    b := protect (a);
    b ();
}

When I compile this I get the error:

cannot use a (type func()) as type func(...interface { }) in function argument

Why is a function without arguments not compatible to a function with a variable number of arguments? What can I do to make them compatible?

Update:
The protected function should be compatible with the original:

func take_func_int_int (f func (x int) (y int)) (int) {
    return f (1)
}

func main () {

    a := func (x int) (y int) {
        return 2 * x
    }
    b := protect (a)

    take_func_int_int (a)
    take_func_int_int (b)
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-22T11:46:02+00:00Added an answer on May 22, 2026 at 11:46 am

    Types are pretty concrete in Go. You could try

    a := func(_ ...interface{}) {
        fmt.Println("unprotected")
    }
    

    func (...interface{}) does not mean “any function that takes any number of any kind of arguments”, it means “only a function which takes a variable number of interface{} arguments”

    Alternatively rather than func(...interface{}) you can just use interface{} and the reflect package. See http://github.com/hoisie/web.go for an example.

    EDIT: Specifically, this:

    package main
    
    import (
        "fmt"
        "reflect"
    )
    
    func protect(oldfunc interface{}) (func (...interface{})) {
        if reflect.TypeOf(oldfunc).Kind() != reflect.Func {
            panic("protected item is not a function")
        }
        return func (args ...interface{}) {
            fmt.Println("Protected")
            vargs := make([]reflect.Value, len(args))
            for n, v := range args {
                vargs[n] = reflect.ValueOf(v)
            }
            reflect.ValueOf(oldfunc).Call(vargs)
        }
    }
    
    func main() {
        a := func() {
            fmt.Println("unprotected")
        }
        b := func(s string) {
            fmt.Println(s)
        }
        c := protect(a)
        d := protect(b)
        c()
        d("hello")
    }
    

    Ouput is

    Protected
    unprotected
    Protected
    hello
    

    EDIT: To answer the update

    Like I said above, types are pretty concrete in Go. The protect function returns a type func(...interface{}) which will never be assignable to func(int)int. I think you’re probably either over-engineering your problem or misunderstanding it. However, here’s a highly discouraged code snippet that would make it work.

    First change protect to also return values:

    func protect(oldfunc interface{}) (func (...interface{}) []interface{}) {
        if reflect.TypeOf(oldfunc).Kind() != reflect.Func {
            panic("protected item is not a function")
        }
        return func (args ...interface{}) []interface{} {
            fmt.Println("Protected")
            vargs := make([]reflect.Value, len(args))
            for n, v := range args {
                vargs[n] = reflect.ValueOf(v)
            }
            ret_vals := reflect.ValueOf(oldfunc).Call(vargs)
            to_return := make([]interface{}, len(ret_vals))
            for n, v := range ret_vals {
                    to_return[n] = v.Interface()
            }
            return to_return
        }
    }
    

    Then make a convert function:

    func convert(f func(...interface{}) (func(int) int) {
        return func(x int) int {
            r := f(x)
            return r[0].(int)
        }
    }
    

    Then your call would look like

    take_func_int_int(convert(b))
    

    But I promise this isn’t what you actually want to do.

    Step back and try to rework the problem. I’ve completely killed type-safety in these examples. What are you trying to accomplish?

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

There is this simple function which I have used with C++ in the past
I learn Python, and bumped into interesting thing: I try to write lambda-function, which
Whenever I try to write graphical programs (whether a game or really any GUI
This snippet works well if I try to write in a user directory but
Can I overload a function which takes either a reference or variable name? For
I can't find a solution for this problem: I write a program, which reads
I have found this example on StackOverflow: var people = new List<Person> { new
I'm trying to write a Java function that takes a Class<?> and returns a
I want to write a function validate() which will take some pattern or regular
I have recently tried to write an application in c# using EWS to read

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.