How to make array of struct and return from a function in golang?

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

How to make array of struct and return from a function in golang?

问题

最简单的方法是使用切片来创建 Process 数组。在你的方法中,你可以声明一个空的 Process 切片,然后在循环中将每个 Process 结构体添加到切片中。最后,返回该切片即可。以下是修改后的代码:

func procData(values []string) ([]Process, error) {
    var processes []Process // 声明一个空的 Process 切片

    for _, value := range values {
        pieces := strings.Split(value, "-")
        var process Process

        if len(pieces) > 1 {
            process = Process{Key: pieces[0], Value: pieces[1]}
        } else if len(pieces) > 2 {
            process = Process{Key: pieces[0], Value: pieces[2]}
        }

        processes = append(processes, process) // 将每个 Process 结构体添加到切片中
    }

    return processes, nil // 返回 Process 切片
}

这样,你就可以通过将每个 Process 结构体添加到切片中来创建 Process 数组,并在方法的最后返回该数组。

英文:

I recently started working with golang and I need to make array of struct. Below is my struct:

type Process struct {
	Key   string
	Value string
}

Now from my method I need to return []Process. Below is my method:

func procData(values []string) ([]Process, error) {
	var process Process
	for _, value := range values {
		pieces := strings.Split(value, "-")
		if len(pieces) > 1 {
			process = Process{pieces[0], pieces[1]}
		} else if len(pieces) > 2 {
			process = Process{pieces[0], pieces[2]}
		}
		// add process struct process array? how to add process struct to make Process array
	}
}

I am confuse on how to make Process array by adding individual process struct into them and then return it.

答案1

得分: 2

使用append函数将结果收集到一个slice中。

func procData(values []string) ([]Process, error) {
    var result []Process
    for _, value := range values {
        var process Process
        pieces := strings.Split(value, "-")
        if len(pieces) > 1 {
            process = Process{pieces[0], pieces[1]}
        } else if len(pieces) > 2 {
            process = Process{pieces[0], pieces[2]}
        }
        result = append(result, process)
    }
    return result
}
英文:

Use append to collect the results in a slice.

func procData(values []string) ([]Process, error) {
    var result []Process
    for _, value := range values {
        var process Process
        pieces := strings.Split(value, "-")
        if len(pieces) > 1 {
            process = Process{pieces[0], pieces[1]}
        } else if len(pieces) > 2 {
            process = Process{pieces[0], pieces[2]}
        }
        result = append(result, process)
    }
    return result
}

答案2

得分: 0

func procData(values []string) ([]Process, error) {
    processList := make([]Process, len(values))
    var process Process
    for _, value := range values {
        pieces := strings.Split(value, "-")
        if len(pieces) > 1 {
            process = Process{pieces[0], pieces[1]}
        } else if len(pieces) > 2 {
            process = Process{pieces[0], pieces[2]}
        }
        // 如何将 process 结构体添加到 processList 中?如何创建 Process 数组?
        processList = append(processList, process)
    }
    return processList
}

以上是一个函数的代码片段,它接受一个字符串数组 values,并返回一个 Process 数组和一个错误。在函数中,它通过将每个字符串按照 "-" 进行分割,创建一个 Process 结构体,并将其添加到 processList 数组中。请问你需要什么帮助?

英文:
func procData(values []string) ([]Process, error) {
    processList := make([]Process, len(values))
    var process Process
    for _, value := range values {
        pieces := strings.Split(value, "-")
        if len(pieces) > 1 {
            process = Process{pieces[0], pieces[1]}
        } else if len(pieces) > 2 {
            process = Process{pieces[0], pieces[2]}
        }
        // add process struct process array? how to add process struct to make Process array
        processList = append(processList, process)
    }
    return processList
}

huangapple
  • 本文由 发表于 2022年1月20日 13:58:03
  • 转载请务必保留本文链接:https://go.coder-hub.com/70781435.html
匿名

发表评论

匿名网友

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

确定