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 8747989
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T12:26:58+00:00 2026-06-13T12:26:58+00:00

When assigning a function to a variable, why does the compiler require a perfect

  • 0

When assigning a function to a variable, why does the compiler require a perfect function signature match when…

  • The variable’s type is a function whose parameter or return is a specific interface, and
  • The function being assigned requires a different interface, but is an interface that embeds the expected interface.

Take this example where…

  • Fooer is an interface
  • FooerBarer is an interface that embeds the Fooer interface
  • *bar implements FooerBarer

http://play.golang.org/p/8NyTipiQak

    // Define a type that is a function that returns a Fooer interface
type FMaker func() Fooer

/* Define values of the FMaker type */

    // This works, because the signature matches the FMaker type
var fmake FMaker = func() Fooer {
    return &bar{}
}

    // This causes an error even though a FooerBarer is a Fooer
var fmake2 FMaker = func() FooerBarer {
    return &bar{}
}

So my question is not about an alternate solution, but rather why the compiler is built this way.

It would seem that the compiler would see that by returning a FooerBarer, you are therefore returning a Fooer, and would accept the assignment.

So…

  • What is the reason for this strict behavior of the compiler?
  • What problem is being solved or danger is being avoided?
  • Why is this any different than the compiler accepting a FooerBarer value in an assignment to a Fooer variable?
  • 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-06-13T12:26:59+00:00Added an answer on June 13, 2026 at 12:26 pm

    To put it simply, a Fooer is not a FooerBarer. Both are interface types, but they point to different itables. A Fooer is guaranteed to have the first method in the itable be Foo() Fooer. In a FooerBarer, it may have Bar() FooerBarer as its first method. So during runtime a method lookup would return the wrong method.

    Any conversion from a FooerBarer to a Fooer is guaranteed to succeed because a FooerBarer always has the method set required for a Fooer. The way interface conversion works, the runtime first looks up the real type of the FooerBarer it has received (such as a bar) and then looks up the itable for the bar/Fooer pair and creates a new interface value.

    In Go code, you can cause this to happen explicitly or implicitly. For example x := Fooer(myFooerBarer). This would do an explicit conversion and place the new interface value in x. If you had a function of type func(Fooer) and passed a FooerBarer, then the conversion would happen implicitly. The compiler would do the conversion and assign the result to the parameter of the function call.

    In your case above, you are attempting to assign a func() FooerBarer to a func() Fooer. In Go, no assignment has an automatic conversion. You can not assign a double to an int. You cannot even assign a time.Duration to an int64 even though their underlying types are identical. In this case, the function would need to be wrapped so that the conversion could be done each time the function was run. Not allowing conversions between the same underlying type to be automatic and automatically wrapping functions would be a bit inconsistent.

    If you really need to do something like this, there is an easy answer. Just wrap the function.

    var fbmake = func() FooerBarer {
        return &bar{}
    }
    
    var fmake Fmaker = func() Fooer {
        return fbmake()
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

When you're assigning a function to a variable, does it make any difference at
I am assigning to a std::function<double()> a lambda expression. This snippet works if(fn_type==exponential) k.*variable
What does assigning a variable to {} , mean? Is that initializing it to
This may sound like a dumb question, but does assigning a variable to a
I have a class, whose __init__ function does a bunch of stuff, and that's
I am using split function and assigning the value in a variable and running
I am calling this function, assigning the result to a variable in the callback
Assigning a Date variable to another one will copy the reference to the same
When assigning a type casted array to codeigniter's session, I get this error: A
I could use some help assigning to a global C variable in DLL using

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.