Skip to content

Instantly share code, notes, and snippets.

@Micrified
Created April 25, 2024 17:44
Show Gist options
  • Save Micrified/41699f4bb94360371df8a3403a4d1960 to your computer and use it in GitHub Desktop.
Save Micrified/41699f4bb94360371df8a3403a4d1960 to your computer and use it in GitHub Desktop.
Gotcha
// Online Go compiler to run Golang program online
// Print "Try programiz.pro" message
package main
import "fmt"
// ---
type Restful interface {
Get() string
Set() string
}
type Method func(Restful) string
type Controller interface {
Restful
Handler(string) Method
}
type controller [T any] struct {
route string
handles map[string]Method
data T
}
// ---
type blog struct {
table string
}
type Blog controller[blog]
func NewBlog() Blog {
return Blog {
route: "blog",
handles: map[string]Method {
"get": Restful.Get,
"set": Restful.Set,
},
data: blog {
table: "table",
},
}
}
func (b *Blog) Handler(m string) Method {
if h, ok := b.handles[m]; ok {
return h
}
return nil
}
func (b *Blog) Get() string {
return "B.Get"
}
func (b *Blog) Set() string {
return "B.Set"
}
// ---
func show(c Controller, m string) {
if h := c.Handler(m); h != nil {
fmt.Println(h(c))
} else {
fmt.Println("None")
}
}
func main() {
b := NewBlog()
show(&b, "get")
// show(&PasteController, "get")
// show(&PasteController, "set")
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment