嵌入何时使用指针 [英] Embedding when to use pointer

查看:128
本文介绍了嵌入何时使用指针的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


$ b 例如


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

 类型Job结构{
命令字符串
* log.Logger
}

 类型Job结构{
命令字符串
log.Logger
}


解决方案

您可以使用其中一种:结构类型,该规范提到:


使用类型声明但没有显式字段名称的字段是匿名字段,也称为嵌入式 em>字段或类型嵌入结构中。

嵌入式类型必须指定为类型名称 T 或指向非接口类型名称 * T T 本身可能不是指针类型。




由于 log.Logger not 接口,您可以使用类型或指向匿名字段类型的指针 Logger






文章嵌入Go 来回 Eric Urban( hydrogen18


    >
  • 这样做的第一个好处是,您可以依赖使用> NewX 成语函数返回一个结构by-pointer来执行初始化的函数。 第二个优点是可以嵌入类型的所有功能,而无需知道何时实例化
    嵌入指向位图的指针与Go中的任何其他指针没有区别,因此它可以分配多次。

    通过这样做,您可以更改实例您在运行时动态扩展。


例如:

 类型位图结构{
data [4] [5] bool
}

类型Renderer结构{
* Bitmap //嵌入指针
on uint8
off uint8
}



Renderer 类型嵌入了一个位图 by-pointer。


一个位图实例可以作为许多 Renderer 实例:



  var renderA,renderB渲染器
re nderA.on ='X'
renderA.off ='O'
renderB.on ='@'
renderB.off ='。'

var pic位图
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 =& amp ; pic

renderA.render()
renderB.render()




它将两个不同的渲染器共享同一个位图实例。

每个渲染器都有自己的一组字符,允许打印位图的两个表示。

这就是输出的样子:



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




这个例子演示了享元模式

尽管在这个例子中对内存消耗无关紧要,但拥有数千个实例共享一个底层数据结构对于减少系统内存消耗非常重要。






此线程


您无法指向指向接口匿名的指针和指针的原因字段是这些类型没有方法。

匿名字段的全部要点是方法得到提升


我已经解释了为什么接口没有方法:很多人使用指针来错误地和不必要的接口,并且没有任何已知的有效用途,所以该语言被改为通过指向接口的指针没有方法来积极地阻止这种用法。



When I want to embed a struct within another struct, should I use a pointer or value?

For example

type Job struct {
    Command string
    *log.Logger
}

or

type Job struct {
    Command string
    log.Logger
}

解决方案

You can use one or the other: for struct type, the spec mentions:

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.

Since log.Logger is not an interface, you can use the type or a pointer to the type for the anonymous field Logger.


The article "Embedding in Go " fro Eric Urban (hydrogen18) calls embedding a pointer "embed by-pointer":

  • 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.

For instance, with:

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

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

The Renderer type embeds a Bitmap by-pointer.

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.


As mentioned in 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.

这篇关于嵌入何时使用指针的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆