如何在Go的结构体中从函数/方法中保存数据?

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

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?
}

huangapple
  • 本文由 发表于 2017年9月6日 23:33:36
  • 转载请务必保留本文链接:https://go.coder-hub.com/46079222.html
匿名

发表评论

匿名网友

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

确定