What I want to achieve is to create a function that simply wraps json.Unmarshal, but I would like to take the structure that I want to pass to Unmarshal as an argument of type interface{}.
However, it has not been realized as follows.What are the options?
Step 1: Base Code
package main
import(
"encoding/json"
"fmt"
)
type User structure {
Name string `json: "name"`
}
funcmain(){
j:=[]byte(`{"name":"ando"}`)
var user User
if err: = json.Unmarshal(j, & user); err!=nil {
panic(err)
}
fmt.Printf("%#v", user)
// =>main.User {Name: "ando"}
}
Step 2: Pass interface{} type as it is and fail
Created an unmarshal function wrapped in json.Unmarshal.However, I passed the interface{} type directly to Unmarshal, so it came back with a map type.
func main(){
bs: = [ ] byte (`{"name":"ando"}`)
var user User
_,err:=unmarshal(bs,user)
if err!=nil{
panic(err)
}
}
funcunmarshal(bs[]byte, st interface{}) (interface{}, error) {
iferr:=json.Unmarshal(bs, & st);err!=nil{
return nil, err
}
fmt.Printf("%#v\n",st)
// = > map [string] interface { } { "name" : "ando" }
return st, nil
}
Step 3: Reflect Package Restore to Original Structure Failed
For the first time using the reflect package, I tried to restore the original structure from type interface{}, but it did not fail to decode properly.
import(
"encoding/json"
"fmt"
"reflect"
)
funcunmarshal(bs[]byte, st interface{}) (interface{}, error) {
// the intention of trying to restore the original structure
rv: = reflect.New(reflect.TypeOf(st)) .Elem()
iferr:=json.Unmarshal(bs, & rv);err!=nil{
return nil, err
}
fmt.Printf("%#v\n",rv)
// =>main.User {Name:"} *The value remains the initial value
return rv, nil
}
This is a self-answer (from Metropolis' comment)
I don't really understand it yet, but I was able to do the following, so I will leave it as an answer for now.
package main
import(
"encoding/json"
"fmt"
"reflect"
)
type User structure {
Name string `json: "name"`
}
funcmain(){
bs: = [ ] byte (`{"name":"ando"}`)
u,err:=unmarshal(bs,User{})
if err!=nil{
panic(err)
}
user: = u. (User)
fmt.Printf("%s", user.Name)
// =>ando
}
funcunmarshal(bs[]byte, st interface{}) (interface{}, error) {
rv:=reflect.New(reflect.TypeOf(st)) .Interface()
iferr:=json.Unmarshal(bs, & rv);err!=nil{
return nil, err
}
return reflect.ValueOf(rv).Elem().Interface(), nil
}
613 GDB gets version error when attempting to debug with the Presense SDK (IDE)
578 Understanding How to Configure Google API Key
583 PHP ssh2_scp_send fails to send files as intended
919 When building Fast API+Uvicorn environment with PyInstaller, console=False results in an error
622 Uncaught (inpromise) Error on Electron: An object could not be cloned
© 2024 OneMinuteCode. All rights reserved.