Golang:将结构体作为函数参数传递

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

Golang: Passing structs as parameters of a function

问题

尝试通过跟随在线课程来自学一些Go语言,并且我想稍微偏离课程,扩展一下我的学习内容。

课程要求我们编写一个简单的函数,使用几个变量,该函数将接收这两个变量并打印出一行文本。所以我写了这样的代码:

func main() {
    var greeting := "hello"
    var name := "cleveland"

    message := printMessage(greeting, name)
    fmt.Println(message)
}

func printMessage(greeting string, name string) (message string) {
    return greeting + " " + name + "!"
}

后来,课程介绍了一种使用可变参数创建伪数组的方法,代码如下:

func sayHello(cities ...string) (message string) {
    for _, city := range cities {
        message := printMessage("hello", city)
        fmt.Println(message)
    }
}

我想创建一个包含不同问候语的结构体,并将其传递给sayHello函数。所以结构体和变量看起来会像这样:

type cityInfo struct {
    greeting     string
    name         string
    wins         float32
    gamesPlayed  float32
}

city1 := cityInfo{"hello", "cleveland"}
city2 := cityInfo{"good morning", "atlanta"}
// ...以此类推

我应该如何修改函数,以便将这些结构体传递给函数,并且可以迭代结构体的数量,并使用city.greetingcity.name获取问候语和名称?这个问题清楚吗?

英文:

Trying to teach myself some Go by following an online course. And I'm trying to go a bit off course to expand on my learning a bit.

The course had us writing a simple function using a couple variables and the function would take the two variables and print out a line. So I had:

    func main() {
        
        var greeting := "hello"
        var name := "cleveland"

        message := printMessage(greeting,name)
        fmt.Println(message)

    }

    func printMessage(greeting string, name string) (message string) {

        return greeting + " " + name + "!"

    }

Later the course introduced a way to create an pseudo-array of strings using the using the

    func sayHello (cities ...string) (message string) {
        for _, city := range cities {
            message := printMessage("hello", city)
            fmt.Println(message)
        }
    }

I would like to create a struct with different greetings and pass those into the sayHello function. So the struct and the variables would looks something like this:

    type cityInfo struct {
        greeting string
        name string
        wins float32
        gamesPlayed float32
    }

    city1 := cityInfo{"hello", "cleveland"}
    city2 := cityInfo{"good morning", "atlanta"}
    ...and so on

How do I format the function to pass those structs into the function so that I can iterate on the number of structs and get the greetings and names using city.greeting and city.name? Does this question make sense?

答案1

得分: 4

函数参数类型可以是任何有效的类型:

func sayHello(cities ...cityInfo) {
    for _, city := range cities {
        message := printMessage(city.greeting, city.name)
        fmt.Println(message)
    }
}
英文:

Function argument type can be any valid type:

func sayHello (cities ...cityInfo) {
    for _, city := range cities {
        message := printMessage(city.greeting, city.name)
        fmt.Println(message)
    }
}

答案2

得分: 0

一种解决方案是创建一个接口和一个问候方法。

例如:

type Greetable interface {
    Greeting() string
    Name() string
}

然后在你的结构体中实现GreetingName方法(由于Go处理接口的方式,这将立即实现Greetable接口):

type cityInfo struct {
    name     string
    greeting string
}

func (city *cityInfo) Greeting() string {
    return city.greeting
}

func (city *cityInfo) Name() string {
    return city.name
}

然后你的函数可以接受任何实现了Greetable接口的对象:

func sayHello(greetables ...Greetable) (message string)

并使用Name()Greeting()方法。

英文:

One solution would be to create an interface and a greeting method.

For example:

type Greetable interface {
    Greeting() string
    Name() string
}

You would then implement the Greeting and Name methods in your struct (this would immediately implement the Greetable interface, due to the way that Go handles interfaces):

type cityInfo struct {
    name     string
    greeting string
}

func (city *cityInfo) Greeting() string {
    return city.greeting 
}

func (city *cityInfo) Name() string { 
    return city.name 
}

And then your function would just accept anything that implements Greetable:

func sayHello(greetables ...Greetable) (message string)

And use the Name() and Greeting() methods instead.

huangapple
  • 本文由 发表于 2017年9月8日 07:14:13
  • 转载请务必保留本文链接:https://go.coder-hub.com/46106394.html
匿名

发表评论

匿名网友

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

确定