如何在Go语言中删除结构体对象?

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

How to delete struct object in go?

问题

假设我有以下结构体:

type Person struct {
    name string
    age  int
}

如果我创建一个该结构体的对象:

person1 := Person{name: "Name", age: 69}

如果我将该对象设置为nil

person1 = nil

这样是行不通的,实际上会导致类型赋值错误。但是对于映射和切片,这样做是可以的。那么,我该如何删除(释放)该对象呢?我查看了delete内置函数的文档,但它只能从给定的映射中删除条目。谢谢。

英文:

Let's say I have the following struct:

type Person struct {
    name string
    age  int
}

If I make an object of that struct

person1 := Person{name: "Name", age: 69}

If I set this object to nil

person1 = nil

it doesn't work, in fact it's a type assignment error, but it works for maps and slices. So, how otherwise would I remove the object i.e deallocate? I looked at the documentation for delete builtin but it removes an entry from a given map. Thanks.

答案1

得分: 74

Go是一种具有垃圾回收机制的语言。你不应该也不能从内存中删除对象。这是垃圾回收器的职责和责任,它会自动地完成这个任务。当对象变得不可访问时,垃圾回收器会正确地从内存中删除对象。

你可以将nil赋值给映射和切片,因为nil是这些类型的有效值(零值)。对于struct类型来说,零值不是nil,而是一个struct值,其中所有字段都具有它们的零值。

如果你想清除或覆盖结构体的值,你可以简单地将另一个结构体值(最好是零值,即空结构体)赋值给它:

person1 := Person{name: "Name", age: 69}
// 使用 person1

// 清除 person1:
person1 = Person{}

但要知道,这不会释放person1分配的内存;正如前面所述,当它变得不可访问时,内存将由垃圾回收器自动释放。

nil也是指针类型的有效值(零值),所以如果person1是指向Person的指针(即*Person),你也可以将nil赋值给它,例如:

person1 := &Person{name: "Name", age: 69}
// 使用 person1

// 清除 person1:
person1 = nil

当将指针设置为nil时,指向的对象也将由垃圾回收器处理。

有关垃圾回收器工作原理的更多详细信息,请参阅https://stackoverflow.com/questions/37382600/golang-cannot-free-memory-once-occupied-by-bytes-buffer/37383604#37383604。

英文:

Go is a garbage collected language. You are not supposed to, and you cannot delete objects from memory. It is the garbage collector's duty and responsibility to do so, and it does this automatically. The garbage collector will properly remove objects from memory when they become unreachable.

You can assign nil to maps and slices because nil is a valid value (the zero value) for those types. The zero value for struct types is not nil but a struct value where all its fields have their zero values.

If you want to clear or overwrite the struct value, you may simply assign another struct value to it, preferably the zero value (an empty struct):

person1 := Person{name: "Name", age: 69}
// work with person1

// Clear person1:
person1 = Person{}

But know that this will not free memory allocated by person1; as wrote earlier, it will be freed automatically by the GC when it becomes unreachable.

nil is also a valid value (the zero value) for pointer types, so if person1 would be a pointer to Person (that is, *Person), you could also assign nil to it, e.g.:

person1 := &Person{name: "Name", age: 69}
// work with person1

// Clear person1:
person1 = nil

When clearing a pointer by setting it to nil, the pointed object–again–will be taken care of by the GC.

For more details about how the garbage collector works, see https://stackoverflow.com/questions/37382600/golang-cannot-free-memory-once-occupied-by-bytes-buffer/37383604#37383604.

答案2

得分: 15

如果你想设置为nil,使用指针而不是值。

person1 := &Person{name: "Name", age: 69}
// 然后你可以设置
person1 = nil
英文:

If you want to set nil use pointer instead of value.

person1 := &Person{name: "Name", age: 69}
// Then you can set
person1 = nil

huangapple
  • 本文由 发表于 2017年2月6日 19:34:11
  • 转载请务必保留本文链接:https://go.coder-hub.com/42066797.html
匿名

发表评论

匿名网友

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

确定