使用Go语言创建具有从数据切片初始化的结构实例。

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

Create Struct instance with initialization from slices of data golang

问题

我有一个包含数组的数据。

data := [][]int{{1,2,3}, {4,5,6}}

还有一个结构体。

type A struct { I, J, K int }

现在我想为结构体A的每个数组创建一个运行时实例,我该如何实现?如果使用反射是一种方法,那么请告诉我如何做?

这只是一个我想向你展示的示例。但是假设结构体A包含从A到Z的26个字段,类型为int,并且我有100个数据片段,我可以使用这些片段创建/初始化我的结构体A,那么如何在不使用点符号的情况下循环遍历字段索引并将该字段从数据片段中赋值给它?

package main

import (
	"fmt"
)

type A struct {
	I, J, K int
}

func main() {
	data := [][]int{
		{1, 2, 3},
		{4, 3, 2},
	}

	var a A
	// 使用参数为每个数据数组创建类型A的实例
	fmt.Println(a)
}

请在此链接上帮助我:https://play.golang.org/p/rYuoajn5Ln

英文:

I have a data with array of array.

> data := [][]int{{1,2,3}, {4,5,6}}

and struct
> type A struct {
I, J, K int
}

Now I want to create instance run time for struct A with each array from data, How do I achieve that? If reflect is a way, then tell how?

This is just an example that I want to show you. But let's say if struct A contains 26 fields from A to Z with type of int and I have 100 slices of data from which I can create/init my struct A, then how it could be possible without using dot notation on struct and just looping over field index and assign that field from slice data?

package main

import (
	"fmt"
)

type A struct {
	I, J, K int
}

func main() {
	data := [][]int{
		{1, 2, 3},
		{4, 3, 2},
	}

	var a A
	// create instance of type A with params
	// initialization for each array in data
	fmt.Println(a)
}

Please help me at this link: https://play.golang.org/p/rYuoajn5Ln

答案1

得分: 1

我不确定这是否是你要找的,但你可以使用简单的循环来创建这些对象:

func main() {
    data := [][]int{
        {1, 2, 3},
        {4, 3, 2},
    }

    for _, intArr := range data {
        a := NewA(intArr)
        // a:= A{I: ints[0], J: ints[1], K: ints[2]}

        fmt.Println(a)
    }
}

完整的解决方案可以在 https://play.golang.org/p/j7fxbmu3jp 找到。

英文:

I'm not sure if that is what you are looking for, but you can create those objects in a simple loop:

func main() {
	data := [][]int{
    	{1, 2, 3},
	    {4, 3, 2},
	}

	for _, intArr := range data {
    	a := NewA(intArr)
        // a:= A{I: ints[0], J: ints[1], K: ints[2]}
	
	    fmt.Println(a)
    }
}

Full solution available at https://play.golang.org/p/j7fxbmu3jp

答案2

得分: 0

这是更紧凑的版本...

只需遍历你的"数据"数组的数组,并从每个索引创建一个新的"A"。

不需要在一个单独的函数中这样做。

for _, arr := range data {
    a := A{I: arr[0], J: arr[1], K: arr[2]}
    
    fmt.Println(a)
}

完整解决方案在这里:https://play.golang.org/p/jyN7f9c-o-

英文:

Here it is, for a more compact version...

Just range over your "data" array of arrays and create a new "A" from each index.

No need to do that in a separate function.

for _, arr := range data {
	a := A{I: arr[0], J: arr[1], K: arr[2]}
	
	fmt.Println(a)
}

Full solution here: https://play.golang.org/p/jyN7f9c-o-

huangapple
  • 本文由 发表于 2016年12月10日 02:35:10
  • 转载请务必保留本文链接:https://go.coder-hub.com/41066798.html
匿名

发表评论

匿名网友

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

确定