英文:
How to save data in Go struct from function/method?
问题
我刚开始学习Go语言,我在使用struct
保存数据方面遇到了困难。从其他语言过来,我了解到Go语言中没有class
的概念。为了实现类似的功能,可以使用struct
,并且可以将函数"添加"到结构体中。所以我写了下面这个简单的程序:
package main
import "fmt"
type MyStruct struct {
the_number int
}
func (self MyStruct) add(another_number int) int {
self.the_number += another_number // 将结果保存到结构体的the_number字段中
return self.the_number
}
func main() {
my_struct := MyStruct{1}
result := my_struct.add(2)
fmt.Println(result) // 输出3,符合预期
fmt.Println(my_struct.the_number) // 输出1。为什么不是3呢?
}
从注释中可以看出,我对于实例化的my_struct
中的self.the_number
没有保存结果感到困惑。
后来我发现可以通过以下方式解决这个问题:
my_struct.the_number = my_struct.add(2)
但是,有时候方法/函数可能会变得复杂,我希望能够从函数内部向my_struct
保存大量的数据。
有没有比我更聪明的人能给我一个提示,告诉我我在这里漏掉了什么?
我该如何在函数内部将数据保存到实例化的struct
中?
英文:
I'm just starting out with Go and I'm having a hard time saving data in a struct
. Coming from other languages I learned there is no such thing as a class
in Go. For similar purposes, the struct
can be used, and functions can be "added" to the struct. So I wrote the following simple program:
package main
import "fmt"
type MyStruct struct {
the_number int
}
func (self MyStruct) add(another_number int) int {
self.the_number += another_number // I save the result to the struct the_number
return self.the_number
}
func main() {
my_struct := MyStruct{1}
result := my_struct.add(2)
fmt.Println(result) // prints out 3 as expected
fmt.Println(my_struct.the_number) // prints out 1. Why not also 3?
}
As you can see from the comments I'm puzzled by the fact that the result is not saved in self.the_number
in the instantiated my_struct
.
So I found out I can get around this by doing
my_struct.the_number = my_struct.add(2)
But I methods/functions can sometimes become complex in which I want to save a lot of data to the my_struct
from within the function.
Could any smarter soul than me give me a tip on what I'm missing here?
How can I save data to an instantiated struct
from within a function?
答案1
得分: 4
你应该在结构体方法func (self *MyStruct) add(another_number int) int
中使用指针,因为没有*
时,变量(self)是按值传递的,而不是按引用传递的。例如,你正在更新原始对象的副本,而这些更改会被丢弃。
这是一个基础知识,在Go之旅中有详细介绍-每个人在开始使用Go编程之前都应该学习它。
另一种选择是从方法中返回self
-这将遵循“不可变”风格,因此你可以编写像my_struct = my_struct.add(1).add(2)
这样的代码。
package main
import "fmt"
type MyStruct struct {
the_number int
}
func (self *MyStruct) add(another_number int) int {
self.the_number += another_number
return self.the_number
}
func main() {
my_struct := MyStruct{1}
result := my_struct.add(2)
fmt.Println(result) // 输出3,符合预期
fmt.Println(my_struct.the_number) // 输出1。为什么不是3呢?
}
英文:
You should use pointer in struct method func (self *MyStruct) add(another_number int) int
as without the *
variable (self) is passed by value, not by references. E.g. you are updating a copy of an original object and this changes are discarded.
It's a basic stuff and well covered in Tour of Go - everyone should take it before starting coding in Go.
Another option would be return the self
from the method - it would follow "immutable" style so you can write code like: my_struct = my_struct.add(1).add(2)
package main
import "fmt"
type MyStruct struct {
the_number int
}
func (self *MyStruct) add(another_number int) int {
self.the_number += another_number
return self.the_number
}
func main() {
my_struct := MyStruct{1}
result := my_struct.add(2)
fmt.Println(result) // prints out 3 as expected
fmt.Println(my_struct.the_number) // prints out 1. Why not also 3?
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论