在golang中创建一个JSON结构的接口。

huangapple go评论80阅读模式
英文:

Creating an interface for a JSON struct in golang

问题

假设我有一个struct,我将json参数数据绑定到它上面,像这样:

type User struct {
  FirstName string `json:"firstName"`
}

属性FirstName必须大写,以便将json值绑定到该结构体。

但是,我还想创建一个接口,接受任何具有类似属性FirstNamestruct。由于FirstName已经被大写并使用,我必须给方法取一个不同的名字。

type NameInterface interface {
    FirstName() string // 不行
    FirstNameValue() string // 可能可以?
}

但是,我觉得为了让它们能够与接口一起工作,必须为所有我的json结构体的每个属性添加一个辅助函数,这似乎非常奇怪。我是否有什么误解或者是否缺少某种编程模式?在go中,如何使json结构体与接口一起工作的最佳方法是什么?

更多(我想要做的事情):

我想将来自我的controllersjson参数解析为structs,然后将该struct数据传递给filters,然后根据参数数据运行sql命令来过滤数据。我想使用接口,这样我就可以将来自不同来源的structs传递给我的filters

英文:

Say I have a struct that I bind json param data to like

type User struct {
  FirstName string `json:"firstName"`
}

The attribute FirstName has to be capitalized so that the json values can be binded to the struct.

But I also want to create an interface to accept any struct that has a FirstName like attribute. Since FirstName is already capitalized and taken, I have to name the method something else.

type NameInterface interface {
    FirstName() string // nope
    FirstNameValue() string // maybe?
}

But it seems really weird to have to add a helper function for each attribute on all my json structs just so they can work with an interface. Is there something I'm misunderstanding or a programming pattern I'm missing? What is the best way to get a json struct to work with interfaces in go?

More (what I am trying to do):

I want to parse json params that are coming from my controllers into structs. And then pass that struct data into filters which then run sql commands to filter data based on the params data. I wanted to use interfaces so I can pass in structs created from different sources into my filters.

答案1

得分: 2

在Go语言中,接口用于指定行为,但你试图使用它们来描述数据。这就是为什么你发现你的方法很困难的原因,也是为什么这可能是错误的方法。

如果不知道你真正想要实现什么,很难给出更具体的答案。但是,如果你想要能够从不同的结构体类型中读取特定命名字段的函数,你可能需要定义一个结构体来描述这些数据,然后将该结构体嵌入到其他结构体类型中。

类似下面的代码可能符合你的需求:

type Person struct {
    FirstName string `json:"firstName"`
}

type User struct {
    Person
    // 其他特定于User的字段
}

type Admin struct {
    Person
    // 其他特定于Admin的字段
}

func Harass(p Person) { }

func main() {
    user := User{Person{"Frank"}}
    Harass(user.Person)
    admin := Admin{Person{"Angelina"}}
    Harass(admin.Person)
}

希望对你有帮助!

英文:

Interfaces in Go specify behaviour, but you're trying to use them to describe data. This is why you're finding your approach to be difficult - and it's also why it's probably the wrong approach.

It's difficult to give a more specific answer without knowing what you're really trying to achieve, but if you, for example, want to be able to have functions that can read specific named fields from different struct types, you'll probably want to define one struct to describe this data and then embed that struct into other struct types.

Something like this might fit your needs:

type Person struct {
    FirstName string `json:"firstName"`
}

type User struct {
    Person
    // other User-specific fields
}

type Admin struct {
    Person
    // other Admin-specific fields
}

func Harass(p Person) { }

func main() {
    user := User{Person{"Frank"}}
    Harass(user.Person)
    admin := Admin{Person{"Angelina"}}
    Harass(admin.Person)
}

huangapple
  • 本文由 发表于 2016年2月21日 10:40:39
  • 转载请务必保留本文链接:https://go.coder-hub.com/35531668.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定