Golang中处理...[]string参数类型的函数用法

Golang中处理…[]string参数类型的函数用法 这是我的代码:

type Dog struct {
Name        string   `json:"name"`
Year_old    float32  `json:"year_old"`
Description []string `json:"description"`
}
type Dogs struct {
Dogs []Dog `json:"Dogs"`
}
func GetMessage(descriptions …[]string) (descModify string){
//这里我需要接收所有Dog对象的Description字段
}

问题是如何将所有Dog.Description的[]string发送到函数GetMessage(descriptions …[]string)。在我的测试中,总是只发送最后一个对象!

谢谢!


更多关于Golang中处理...[]string参数类型的函数用法的实战教程也可以访问 https://www.itying.com/category-94-b0.html

3 回复

好的,诺伯特, 我正在测试。稍后我会告诉你结果。

更多关于Golang中处理...[]string参数类型的函数用法的实战系列教程也可以访问 https://www.itying.com/category-94-b0.html


在一个循环中遍历你所有的狗,将它们的名字 append 到一个切片中,然后使用 ... 将该切片传递给 GetMessage

如果这不起作用,请向我们展示你根据我的英文描述实现的 Go 代码。

在Go语言中,...[]string 参数类型表示可变数量的 []string 切片参数。根据你的代码结构,你需要将多个 Dog.Description 切片传递给 GetMessage 函数。问题可能出在如何正确构建参数列表。

以下是正确的实现方式:

package main

import (
    "fmt"
    "strings"
)

type Dog struct {
    Name        string   `json:"name"`
    Year_old    float32  `json:"year_old"`
    Description []string `json:"description"`
}

type Dogs struct {
    Dogs []Dog `json:"Dogs"`
}

func GetMessage(descriptions ...[]string) string {
    var allDescriptions []string
    
    for _, desc := range descriptions {
        allDescriptions = append(allDescriptions, desc...)
    }
    
    return strings.Join(allDescriptions, ", ")
}

func main() {
    dogs := Dogs{
        Dogs: []Dog{
            {
                Name:        "Buddy",
                Year_old:    3.5,
                Description: []string{"friendly", "energetic", "loyal"},
            },
            {
                Name:        "Max",
                Year_old:    5.0,
                Description: []string{"calm", "protective", "intelligent"},
            },
            {
                Name:        "Charlie",
                Year_old:    2.0,
                Description: []string{"playful", "curious", "small"},
            },
        },
    }
    
    // 正确传递多个 []string 参数的方式
    result := GetMessage(
        dogs.Dogs[0].Description,
        dogs.Dogs[1].Description,
        dogs.Dogs[2].Description,
    )
    
    fmt.Println(result)
    // 输出: friendly, energetic, loyal, calm, protective, intelligent, playful, curious, small
}

如果你需要动态处理不确定数量的Dog对象,可以使用以下方法:

func GetDogsMessage(dogs []Dog) string {
    // 将每个Dog的Description转换为单独的参数
    descArgs := make([][]string, len(dogs))
    for i, dog := range dogs {
        descArgs[i] = dog.Description
    }
    
    // 使用...展开切片作为可变参数
    return GetMessage(descArgs...)
}

func main() {
    dogs := Dogs{
        Dogs: []Dog{
            {
                Name:        "Buddy",
                Year_old:    3.5,
                Description: []string{"friendly", "energetic"},
            },
            {
                Name:        "Max",
                Year_old:    5.0,
                Description: []string{"calm", "protective"},
            },
        },
    }
    
    result := GetDogsMessage(dogs.Dogs)
    fmt.Println(result)
    // 输出: friendly, energetic, calm, protective
}

或者直接在 GetMessage 函数中处理 []Dog

func GetMessageFromDogs(dogs []Dog) string {
    var allDescriptions []string
    
    for _, dog := range dogs {
        allDescriptions = append(allDescriptions, dog.Description...)
    }
    
    return strings.Join(allDescriptions, ", ")
}

func main() {
    dogs := Dogs{
        Dogs: []Dog{
            {
                Name:        "Buddy",
                Year_old:    3.5,
                Description: []string{"friendly", "energetic"},
            },
            {
                Name:        "Max",
                Year_old:    5.0,
                Description: []string{"calm", "protective"},
            },
        },
    }
    
    result := GetMessageFromDogs(dogs.Dogs)
    fmt.Println(result)
}

关键点:

  1. ...[]string 接收的是多个独立的 []string 切片
  2. 传递参数时需要明确列出每个切片:GetMessage(desc1, desc2, desc3)
  3. 如果要从切片中展开,需要使用 descriptions... 语法
  4. 在函数内部,descriptions 是一个 [][]string 类型的切片
回到顶部