英文:
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.greeting
和city.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
}
然后在你的结构体中实现Greeting
和Name
方法(由于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.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论