英文:
Same method on different types and return different type value in Go
问题
我已经看到了类似的问题(在Go中对不同数组类型使用相同的方法)。
但在我的情况下,我的函数不返回相同的类型。
你能否将下面的代码简化一些?
package main
import (
"encoding/json"
"fmt"
)
type A struct {
Name string `json:"name"`
Age int `json:"age"`
}
type B struct {
Name string `json:"name"`
Age int `json:"age"`
Address string `json:"address"`
}
func UnmarshalA(b []byte) *A {
var t *A
_ = json.Unmarshal(b, &t)
return t
}
func UnmarshalB(b []byte) *B {
var t *B
_ = json.Unmarshal(b, &t)
return t
}
func main() {
a := []byte(`{"name": "aaaa", "age": 1}`)
unmarshal_a := UnmarshalA(a)
fmt.Println(unmarshal_a.Name)
b := []byte(`{"name": "bbbb", "age": 2, "address": "b@example.com"}`)
unmarshal_b := UnmarshalB(b)
fmt.Println(unmarshal_b.Name)
}
输出结果为:
aaaa
bbbb
链接:https://play.golang.org/p/PF0UgkbSvk
英文:
I've seen some similar question(Same method on different array types in Go)
But in my case my functions do not return same types.
Can you write the below code more simply?
package main
import (
"encoding/json"
"fmt"
)
type A struct {
Name string `json:"name"`
Age int `json:"age"`
}
type B struct {
Name string `json:"name"`
Age int `json:"age"`
Address string `json:address`
}
func UnmarshalA(b []byte) *A {
var t *A
_ = json.Unmarshal(b, &t)
return t
}
func UnmarshalB(b []byte) *B {
var t *B
_ = json.Unmarshal(b, &t)
return t
}
func main() {
a := []byte(`{"name": "aaaa", "age": 1}`)
unmarshal_a := UnmarshalA(a)
fmt.Println(unmarshal_a.Name)
b := []byte(`{"name": "bbbb", "age": 2, "address": "b@example.com"}`)
unmarshal_b := UnmarshalB(b)
fmt.Println(unmarshal_b.Name)
}
// aaaa
// bbbb
答案1
得分: 2
你有几个选择。
-
不要费心使用
UnmarshalA
和UnmarshalB
。它们实际上没有做太多事情,你只是在抽象出一行代码...var t *A
。 -
如果你实际上不需要
A
和B
结构体,只是想要以可用的方式表示JSON字符串的内容,你可以将其解组为map[string]interface{}
。
例如:
package main
import (
"encoding/json"
"fmt"
)
func UnmarshalAny(b []byte) map[string]interface{} {
var t = make(map[string]interface{})
_ = json.Unmarshal(b, &t)
return t
}
func main() {
a := []byte(`{"name": "aaaa", "age": 1}`)
unmarshal_a := UnmarshalAny(a)
b := []byte(`{"name": "bbbb", "age": 2, "address": "b@example.com"}`)
unmarshal_b := UnmarshalAny(b)
// 访问方式如下...
fmt.Println(unmarshal_a["name"])
fmt.Println(unmarshal_b["name"])
}
如果你想通过引用传递数据,你可以将代码更改为以下形式:
package main
import (
"encoding/json"
"fmt"
)
func UnmarshalAny(b []byte) *map[string]interface{} {
var t = make(map[string]interface{})
_ = json.Unmarshal(b, &t)
return &t
}
func main() {
a := []byte(`{"name": "aaaa", "age": 1}`)
unmarshal_a := UnmarshalAny(a)
b := []byte(`{"name": "bbbb", "age": 2, "address": "b@example.com"}`)
unmarshal_b := UnmarshalAny(b)
// 访问方式如下...
fmt.Println((*unmarshal_a)["name"])
fmt.Println((*unmarshal_b)["name"])
}
英文:
You have a couple of options.
-
Don't bother using
UnmarshalA
andUnmarshalB
.
They're really not doing much and you're really only abstracting away a single line...var t *A
-
If you don't actually need the
A
andB
structs and simply want the contents of the JSON string represented in a way you can use it, you could just unmarshal into amap[string]interface{}
.
E.g.
package main
import (
"encoding/json"
"fmt"
)
func UnmarshalAny(b []byte) map[string]interface{} {
var t = make(map[string]interface{})
_ = json.Unmarshal(b, &t)
return t
}
func main() {
a := []byte(`{"name": "aaaa", "age": 1}`)
unmarshal_a := UnmarshalAny(a)
b := []byte(`{"name": "bbbb", "age": 2, "address": "b@example.com"}`)
unmarshal_b := UnmarshalAny(b)
// Accessed like this...
fmt.Println(unmarshal_a["name"])
fmt.Println(unmarshal_b["name"])
}
https://play.golang.org/p/KaxBlNsCDR
If you wanted to pass the data by reference then you would change it to something like this:
package main
import (
"encoding/json"
"fmt"
)
func UnmarshalAny(b []byte) *map[string]interface{} {
var t = make(map[string]interface{})
_ = json.Unmarshal(b, &t)
return &t
}
func main() {
a := []byte(`{"name": "aaaa", "age": 1}`)
unmarshal_a := UnmarshalAny(a)
b := []byte(`{"name": "bbbb", "age": 2, "address": "b@example.com"}`)
unmarshal_b := UnmarshalAny(b)
// Accessed like this...
fmt.Println((*unmarshal_a)["name"])
fmt.Println((*unmarshal_b)["name"])
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论