Saturday, December 2, 2023
HomeGolangInterface with fields - Getting Assist

Interface with fields – Getting Assist

Is there any hack for interfaces to have fields?
The interface with operate definition is sweet for more often than not.

Is there any likelihood?
Why go has determined to maintain interfaces solely with capabilities?

Hello @Mahesh_Kumar1, welcome to the discussion board.

The aim of interfaces is to declare a set of capabilities. Interfaces serve for describing conduct, and never for storing state.

If you wish to have fields, you need to use structs.

What use case do you may have the place you assume you can not use structs?

I perceive the thought.
I used to be pondering one thing else which confused me.

I used to be pondering…
I’m sending some states to the online. I’ve an interface with some behaviour. The struct is simply implementing the behaviour. It has some fields.

I used to be pondering to power the struct to maintain the minimal required fields as outlined within the interface.


In my major func, i’ve handlers. Say, adminhandler, userhandler and few extra.

Each handler should use *template.Template variable

How do i power all of the handlers fulfill this situation?

In future, they need to fulfill through the use of logger, db and extra.

I can alternatively use middleware.
Is that this a great motive to have fields in interface?

I start to midway perceive the issue, however I nonetheless don’t have any clear image.

Are you able to share some minimal instance code to indicate how the handers use *template.Template?

And a extra philosophical query: Does the situation need to be baked into code? Or would it not be adequate to speak the situation within the documentation, as a conference?

(Just like Go’s error dealing with, which can be a conference and never enforced by the compiler.)



Please enter your comment!
Please enter your name here

Most Popular

Recent Comments