英文:
Can I pass in a "Type" as a function parameter?
问题
我正在尝试构建一个库,可以自动将结构类型作为RESTful资源提供。
以下是我在调用代码中设想的样子:
package main
import (
"fmt"
"github.com/sergiotapia/paprika"
)
type Product struct {
Name string
Quantity int
}
func main() {
// 通过提供路由、结构类型和可选的自定义资源管理器,你需要将资源附加到Paprika。
paprika.Attach("/products", Product{}, nil)
paprika.Start(1337)
log.Print("Paprika is up and running.")
}
在我的库中,我正在尝试创建Attach
函数:
package paprika
import (
"fmt"
)
func Attach(route string, resource interface{}, manager ResourceManager) {
}
func Start(port int) {
}
type ResourceManager interface {
add() error
delete() error
update(id int) error
show(id int) error
list() error
}
我如何接受任何结构体的"Type"?我的最终目标是使用反射获取类型名称和字段(这部分我已经知道如何做了)。
对于如何处理这个问题,你有什么建议吗?
英文:
I'm trying to build a library that'll serve a struct Type as a RESTful resource automatically.
Here's what I envision it to look like in the calling code:
package main
import (
"fmt"
"github.com/sergiotapia/paprika"
)
type Product struct {
Name string
Quantity int
}
func main() {
// You need to attach a resource by giving Paprika your route,
// the struct type and optionally a custom resource manager.
paprika.Attach("/products", Product, nil)
paprika.Start(1337)
log.Print("Paprika is up and running.")
}
Inside of my library, I'm trying to create the Attach function:
package paprika
import (
"fmt"
)
func Attach(route string, resource Type, manager ResourceManager) {
}
func Start(port int) {
}
type ResourceManager interface {
add() error
delete() error
update(id int) error
show(id int) error
list() error
}
How can I accept any "Type" of struct? My end goal is to use reflection to get the type name and it's fields (this part I already know how to do).
Any suggestions on how to approach this?
答案1
得分: 1
我找到的一种方法是:
func Attach(route string, resource interface{}) {
fmt.Println(route)
fmt.Println(reflect.TypeOf(resource))
}
然后我可以使用任何类型:
type Product struct {
Name string
Quantity int
}
func main() {
Attach("/products", new(Product))
}
结果为:
/products
*main.Product
除非有更符合惯用方式的方法,否则我认为我找到了解决方案。
英文:
An approach I found is:
func Attach(route string, resource interface{}) {
fmt.Println(route)
fmt.Println(reflect.TypeOf(resource))
}
Then I can use any type I want:
type Product struct {
Name string
Quantity int
}
func main() {
Attach("/products", new(Product))
}
Results in:
/products
*main.Product
Unless there's a more idiomatic way to go about this, I think I found my solution.
答案2
得分: 0
你可以在函数的参数类型中使用interface{}
。然后,通过使用类型断言(type switch),就可以很容易地知道参数的真实类型。
func MyFunc(param interface{}) {
switch param.(type) {
case Product:
DoSomething()
case int64:
DoSomethingElse()
case []uint:
AnotherThing()
default:
fmt.Println("不支持的类型!")
}
}
在上面的代码中,根据param
的类型进行不同的操作。如果param
的类型是Product
,则执行DoSomething()
;如果是int64
,则执行DoSomethingElse()
;如果是[]uint
,则执行AnotherThing()
;否则打印"不支持的类型!"。
英文:
You could probably use an interface{}
as parameter type for your function. Then, it would be quite easy to know the real type of your parameter by using a type switch.
func MyFunc(param interface{}) {
switch param.(type) {
case Product:
DoSomething()
case int64:
DoSomethingElse()
case []uint:
AnotherThing()
default:
fmt.Println("Unsuported type!")
}
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论