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

  • Home
  • SEARCH
  • 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 6247873
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T12:57:18+00:00 2026-05-24T12:57:18+00:00

I have the following construct: type Foo struct { Bar func(foo *Foo) bool }

  • 0

I have the following construct:

type Foo struct {
    Bar func(foo *Foo) bool
}

Because Bar is not really a method, it accepts Foo as parameter (like self in Python’s bound methods). I’d however bind it to the struct as a method if there was an easy way. I suspect I could using reflection, but I want to keep it trivial. Is there a trick to bind a function to a struct? How would you do it?

Edit: I’ll add a concrete example of what I’m doing.

type Route struct {
    Matcher func(route *Route, r *http.Request) bool
}

Route accepts a custom Matcher function. If not set, a default matcher function is set when the route is registered:

func (mux *ServeMux) HandleRoute(r Route) {
    // ...

    // Set default matcher.
    if r.Matcher == nil {
        r.Matcher = mux.RouteMatcher
    }

    // ...
}

Then that function is used to do a matching:

func (mux *ServeMux) Match(r *http.Request) Route {
    // ...

    if route.Matcher(&route, r) {
        ...
    }

    ...
}

The function is not bound to the route. My question is if this is a reasonable/idiomatic way to set a custom callable, or if there is a trick to make the function “bound” to the struct as a method.

  • 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-24T12:57:19+00:00Added an answer on May 24, 2026 at 12:57 pm

    You don’t explain what you are trying to accomplish. What is wrong with this?

    package main
    
    import "fmt"
    
    type Foo struct{}
    
    func (f *Foo) Bar() bool {
        return true
    }
    
    func main() {
        var f Foo
        fmt.Println(f.Bar())
    }
    

    Output:

    true
    

    Are you trying to do something like this?

    package main
    
    import "fmt"
    
    type BarFunc func(foo *Foo) bool
    
    type Foo struct {
        BarFunc
    }
    
    func (f *Foo) Bar() bool {
        return f.BarFunc(f)
    }
    
    func UserBarFunc(foo *Foo) bool { return true }
    
    func main() {
        var f Foo
        f.BarFunc = UserBarFunc
        fmt.Println(f.Bar())
    }
    

    Output:

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

Sidebar

Related Questions

I want to construct the following XML: <?xml version=1.0?> <foo> <bar type=alpha> <first />
Have a method with the following signature: def foo(self, bar, *uks): return other_method(..., uks)
I have the following code where i construct json data and send to the
Suppose I have the following code: class siteMS { ... function __CONSTRUCT() { require
I have a class which has the following constructor public DelayCompositeDesigner(DelayComposite CompositeObject) { InitializeComponent();
I have in my Form constructor, after the InitializeComponent the following code: using (WebClient
I have following situation: I have loged user, standard authentication with DB table $authAdapter
I have following string String str = replace :) :) with some other string;
I have following foreach-loop: using System.IO; //... if (Directory.Exists(path)) { foreach(string strFile in Directory.GetFiles(path,
I have following situation. A main table and many other tables linked together with

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.