-1

There is an answered question which will help you understand what exactly I want to say.

How does the function passed to http.HandleFunc get access to http.ResponseWriter and http.Request?

There are many built-in Go functions where the function parameters get assigned this way. I want to use that coding style in my daily coding life.

I want to write a similar function/method which will get its parameter values from somewhere just like http.Handlefunc's w and r.

func (s SchoolStruct) GetSchoolDetails(name string){
// here the parameter "name" should get assigned exactly like http.HandleFunc()'s "w" and "r".
}
7
  • 1
    Declare a func type, or an interface type with a method, that has the particular signature you need, e.g. in the above it would be type MyHandlerFunc func(string)/type MyHandler interface { GetSchoolDetails(string) }. Then write a router that invokes the correct handler with the parsed input. Then write a server that takes some input parses that into the valid handler input, and then passes that parsed input to the router. Example snippet you can find in net/http package (Go is open sourced). Commented Mar 23, 2022 at 15:55
  • 1
    However your wish to write something like GetXyz(string) using the net/http handler pattern seems very much misguided. The wrong tool for the job. You might want to rethink and redisgn. Commented Mar 23, 2022 at 15:57
  • @mkopriva, I want to write normal functions which will get the function parameters from somewhere else. When we create an API's function we never pass w and r manually(the question link in my explaination). I want that exact coding style. This has nothing to do with http package or router. But the coding style is what I am looking for. Commented Mar 23, 2022 at 16:02
  • The concepts server & router do not imply http. I never mentioned anything about http (other than the fact that it's open sourced and you can take a look at it if what you need is an example of how that design pattern is implemented). Commented Mar 23, 2022 at 16:09
  • You can replace the server&router with something simpler or something more complex. That will depend on where the input is coming from, and on what the raw input looks like and whether or not it needs to be processed before passed to the function. You should include these things in your question. For example how, or by what, do you expect GetSchoolDetails to be triggered? Some event listener? Or maybe a job scheduler? In the net/http package the w and r are prepared by the server and passed through the router to the handler. Which handler is executed depends on the incoming request... Commented Mar 23, 2022 at 16:33

1 Answer 1

2

What http does is that it registers a callback and uses it when the time comes. You don't have to pass the arguments it takes, as servers implementation provides these arguments with correct state. If you want to copy this approach, first you have to ask:

Is there some kind of generic abstraction that computes these parameters? Is the function I write just reacting to something? Does this function have any side effects? Does it return value back to the system?

This approach is very good when you are modifying existing system, extending its behavior with independent units. So to speak, integrating into robust API.

You may be correct that this is a style of doing things, but you cannot use this style on everything. Its just too specific and good at certain group of tasks.

As @mkopriva pointed out, declaring rules and requirements, your logic should satisfy, is known way to execute this style in Go. You have to realize that your logic, encapsulated behind function pointer or interface, has to be passed and controlled by some other code you call indirectly.

I cannot possibly imagine going to such lengths when all components of the system are under your control and system has only one logic to run.

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

1 Comment

thanks a lot for the answer. Cleared most of my doubts.

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.