Golang json: cannot unmarshal object into Go value of type error

other people"s project code is as follows

jsonStr := `{"Name":"lepig", "Age":100}`
    abc := []byte(jsonStr)

    var result []string
    if err := json.Unmarshal(abc, &result); err != nil {
        fmt.Println(err)
        return
    }
    fmt.Println(result)

in learning golang, look at another project, json parsing, I don"t understand it here. My test code runs with an error. But I can change the
var result [] string in my test code to var result interface {} . What I don"t understand is why the var seedUrls [] string declared in other people"s projects can be

.

PS: has the foundation of php, so I feel that I can"t understand a lot of things in learning golang quickly. Don"t know how to get started

Mar.02,2021

{"Name": "lepig", "Age": 100} is JSON Object and cannot be deserialized to [] string . For example, ["a", "b", "c"] can be deserialized to [] string .


you need to define a structure because golang is a static strongly typed language


since your json data is passing an object, the accepted Go type should be a structure.
the code in someone else's project should pass in b, which should be json data in the form of a string array, for example: ["abcd", "1234"] , so it can be received using [] string of type Go.
for var result interface used by landlords {} interface can be used because it can receive any type.

modify method:
define a structure type

type User struct{
    Name string
    Age    int
}
var result User 
if err := json.Unmarshal(abc, &result); err != nil {
        fmt.Println(err)
        return
    }
fmt.Println(result)
Menu