无法从Json响应中获取1个对象

This question already has an answer here:

I am a beginner to Golang. Can you help me with call of function. Here is an example:

package main

import (
    "fmt"
    "net/http"
)

type Info struct {
    Name string  `json:"name"`
    Year float64 `json:"year,string"`
}

func (b *Base) GetInfo() (Info, error) {
    var resp Info
    path := "example.com"

    return resp, http.Get(path)
}

func main() {
    test, err := Base.GetInfo()
    fmt.Println(test, err)

}

Output:

{Name Bob Year 10}

How I can get only "Bob" ?

And If my response contains more objects example

{Name Bob Year 10}{Name Jane Year 2}.

How I can get only names? Didnt understand how to decode it or call.

</div>

Use . parameter to get name from resp like

func main() {
    test, err := Base.GetInfo()
    fmt.Println(test.Name, err)
}

But if there are multiple fields. Then you should save them in array of struct Info as:

func (b *Base) GetInfo() ([]Info, error) {
    var resp []Info
    path := "example.com"
    return resp, http.Get(path)
}

func main() {
    test, err := Base.GetInfo()
    fmt.Println(test[0].Name, err)
}