But in that way I will have redirection to various http method in Handle function which will get repeated for all handler function (here Handle2, Handle1).
Want i want from my repo design is :
Handlers attached to a uri have http method handller function implementation internally
I’m not sure if understand your problem correctly.
For every action on API, you’ll have a separate HandleUserCreate/HandleUserBan function but you’ll have a shared struct between them.
You cannot eliminate calling http.Handle* functions.
Why don’t you just use mux? You can easily register routes mapping URL paths to handlers. If an incoming request URL matches one of the paths, the corresponding handler is called passing (http.ResponseWriter, *http.Request) as parameters. Paths can also have variables. They are defined using the format {name} or {name:pattern} . If a regular expression pattern is not defined, the matched variable will be anything until the next slash. Here is a quick example:
It looks you want to build a REST app with some kind of automation/magic. In Go, it’s promoted to do things more explicit, that’s why you won’t find almost nothing that does that.
Using mux is a good idea but I’m not sure if it solves your problem the way you want. You can achieve what you want using reflect package, take a look at the signature of the function and register proper handlers.
In comparison to python, Go is a compiled/statically typed language and this is the reason why it’s harder but - possible. Let me know if you found answer to your question or have problems with implementation.
Go doesn’t work this way. We don’t have inherence but composition. It means that in B you can find A but B doesn’t inherence from A anything. The print statement refers to print function which is in A and Go tries to use this one.
I’m wondering why you’re trying to do what you’re doing. The Go way is something like this one
package main
import "fmt"
type helloer interface {
hello() string
}
type A struct {
}
func (a A) hello() string {
return "Hello A"
}
type B struct {
}
func (b B) hello() string {
return "Hello B"
}
type printer struct {
h helloer
}
func (a *printer) print() {
fmt.Println(a.h.hello())
}
func main() {
a := printer{A{}}
a.print()
b := printer{B{}}
b.print()
}