将一个大的结构体缩小为一个小的结构体?

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

Reduce a large struct into a small one?

问题

假设我调用了一个 API,它返回一个结构体切片,每个结构体都有很多方法和字段,但我只想使用返回值中每个元素的一个字段。我该如何做到这一点?

例如,我调用一个 API,它返回一个包含 x 个元素的切片,每个元素有 4 个值和 13 个方法,但我只想要一个值和零个方法(即胎儿结构体的切片)。我该如何将其转换为我自己的结构体?例如:

func GETApi() []fetus {
    // doGet() 返回一个 persons 的切片,其结构如下所示
    a := doGet()  // 这将得到一个包含详细 persons 的切片,但我只想要一个 fetus 的切片

    /*
    type person struct {
        id: 
        age:
        height: 
        width:
    }

    func (a *person) GetHeight() int { ... }
    func (a *person) GetWidth() int { ... }
    func (a *person) GetLaught() int { ... }

    // 我想要返回这些的切片 ([]fetus)
    type fetus struct {
        id: 
    }

    var f fetus 
    f := a  // 我该如何将 persons 的切片压缩成 fetus 的切片
    return f
}

你可以使用类型转换来将 []person 转换为 []fetus。在这种情况下,你可以使用一个循环来遍历 a 中的每个元素,并将其转换为 fetus 类型的元素,然后将其添加到一个新的切片中。以下是一个示例代码:

func GETApi() []fetus {
    a := doGet()

    var f []fetus
    for _, p := range a {
        f = append(f, fetus{id: p.id})
    }

    return f
}

这样,你就可以将 []person 转换为 []fetus,并只保留 id 字段。

英文:

Say I make a call to an api, which returns a slice of structs, each with a load of methods and fields, but I only want to use one field per each element of the returned values. How can I do this?

For instance, I call out to an API, and it returns a slice of x elements, each which has 4 values and 13method, but I only want 1 value and 0 methods (the slice of fetus structs). How can I marshall this into my own struct? eg:

func GETApi() []fetus {
//doGet() returns a slice of persons, which are described below 
a := doGet()  // this gets many detailed persons as a slice, but I just want them as a slice of fetus
/*
 type person struct {
  id: 
  age:
  height: 
  width:
 }

func (a *person) GetHeight() int { ... }
func (a *person) GetWidth() int { ... }
func (a *person) GetLaught() int { ... }

// I want to return a slice of these ([]fetus)
type fetus struct {
  id: 
 }
var f fetus 
f := a  // how can I condense said slice of persons into a slice of fetus
return f
*/

</details>


# 答案1
**得分**: 2

也许是这样的?

    package main
    
    type Person struct {
        Id string
        Name string
        Age string
        LotsOfOtherFields string
    }
    
    type Fetus struct {
        Id string
    }
    
    func main() {
        persons := []Person{
            {Id: "a", Name: "John"},
            {Id: "b", Name: "Steve"},
            {Id: "c", Name: "Fred"},
        }
    
        fetuses := make([]Fetus, len(persons))
        for i, p := range persons {
            // 创建一个新的胎儿结构体,并从人的结构体中获取ID
            fetuses[i] = Fetus{Id: p.Id}
        }
    }

<details>
<summary>英文:</summary>

Perhaps something like this?

    package main
    
    type Person struct {
    	Id string
    	Name string
    	Age string
    	LotsOfOtherFields string
    }
    
    type Fetus struct {
    	Id string
    }
    
    func main() {
    	persons := []Person{
    		{Id: &quot;a&quot;, Name: &quot;John&quot;},
    		{Id: &quot;b&quot;, Name: &quot;Steve&quot;},
    		{Id: &quot;c&quot;, Name: &quot;Fred&quot;},
    	}
    
    	fetuses := make([]Fetus, len(persons))
    	for i, p := range persons {
    		// Create a new fetus struct and pluck the ID from the person struct
    		fetuses[i] = Fetus{Id: p.Id}
    	}
    }

</details>



huangapple
  • 本文由 发表于 2021年6月4日 09:19:28
  • 转载请务必保留本文链接:https://go.coder-hub.com/67830354.html
匿名

发表评论

匿名网友

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

确定