go - 嵌入何时使用指针

标签 go embedding

当我想将一个结构嵌入另一个结构时,我应该使用指针还是值?

例如

type Job struct {
    Command string
    *log.Logger
}

type Job struct {
    Command string
    log.Logger
}

最佳答案

您可以使用其中之一:for struct type ,规范提到:

A field declared with a type but no explicit field name is an anonymous field, also called an embedded field or an embedding of the type in the struct.

An embedded type must be specified as a type name T or as a pointer to a non-interface type name *T, and T itself may not be a pointer type.

log.Logger 不是接口(interface),您可以使用匿名字段 Logger 的类型或指向该类型的指针。


文章“Embedding in Go ”来自Eric Urban (hydrogen18)调用嵌入指针“通过指针嵌入”:

  • The first advantage to this is that you can rely on functions that use the NewX idiom returning a struct by-pointer to do initialization.
  • The second advantage is that you can embed all the functionality of a type without needing to know when it is instantiated.
    The embedded pointer to a Bitmap is no different than any other pointer in Go, so it can be assigned multiple times.
    By doing this you can change what instance you are extending dynamically at run time.

例如:

type Bitmap struct{
    data [4][5]bool
}

type Renderer struct{
    *Bitmap //Embed by pointer
    on uint8
    off uint8
}

Renderer 类型通过指针嵌入一个Bitmap

A single instance of Bitmap can act as the embedded instance of many Renderer instances:

var renderA,renderB Renderer
renderA.on = 'X'
renderA.off = 'O'
renderB.on = '@'
renderB.off = '.'

var pic Bitmap
pic.data[0][6] = true
pic.data[0][7] = true
pic.data[1][8] = true
pic.data[2][9] = true
pic.data[3][10] = true

renderA.Bitmap = &pic
renderB.Bitmap = &pic

renderA.render()
renderB.render()

This shares the same Bitmap instance to two different renderers.
Each renderer has its own set of characters, allowing two representations of the bitmap to be printed.
This is what the output looks like:

OXXO
OXOO
OXOO
OXOO
.@@.
.@..
.@..
.@..

This example demonstrates the Flyweight Pattern.
Although inconsequential to memory consumption in this example, having many thousands of instances sharing a single underlying data structure can be very significant in reducing the memory consumption of systems.


this thread 中所述:

The reason why you can't have pointer to pointer and pointer to interface anonymous fields is that these types don't have methods.
The whole point of anonymous fields is that methods get promoted.

I already explained why interfaces don't have methods: a lot of people were using pointers to interfaces incorrectly and unnecessarily, and there weren't any known valid uses, so the language was changed to actively discourage this usage by making pointers to interfaces have no methods.

关于go - 嵌入何时使用指针,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27733854/

相关文章:

date - Go:比较两个只考虑日期的时间结构

python - 如何对在 NetworkX 中创建的图 g 进行聚类?

html - 如何在 HTML 文档中嵌入透明的 SVG 对象?

information-retrieval - 存储一组点(嵌入)的最有效方法是什么,以便快速计算最近点的查询

c++ - 使用宿主 C++ 应用程序编译 Lua 源代码

actionscript-3 - ActionScript 3 和字体嵌入

go - 如何将带时区的日期转换为 Javascript ISO 格式?

string - 通过强制转换和使用串联运算符将整数附加到字符串

arrays - 在 Go 中打印数组值

go - golang 的手册页?