关于Kubernetes SDK方法返回指针类型的问题。

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

about k8s sdk method return pointer type

问题

当一个方法返回指针类型时,调用者可以直接使用方法的返回值,而不需要使用*来获取该值。在你提供的示例代码中,方法返回的是指针类型的pods,所以调用者可以直接使用pods而不是*pods

示例代码中的方法定义如下:

func (c *Clientset) CoreV1() corev1.CoreV1Interface {
    return c.coreV1
}

这个方法返回的是corev1.CoreV1Interface类型的指针,所以调用者可以直接使用pods而不是*pods

希望能帮到你!如果还有其他问题,请随时提问。

英文:

I learn k8s sdk source code has a question,when a method return point type,the caller can
direct use method return value without using * get the value, the demo code
关于Kubernetes SDK方法返回指针类型的问题。

the method defined
关于Kubernetes SDK方法返回指针类型的问题。

why, the demo code,direct use pods,not *pods

答案1

得分: 1

这是Go语言与其他(大多数较旧的)C语言家族语言之间的一个区别之一。在某些语言中,你需要使用专用的运算符来访问通过间接方式(即指针)访问对象的字段,在Go语言中,点运算符同时处理这两种情况。

如果有人偶然遇到这个问题并不知道这一切是什么意思:如果你在C语言中有一个指向这样一个类型的指针:

typedef struct pod_list {
    item items[100]; // 或其他内容
} pod_list;

你需要写以下代码来访问items数组:

item * an_item = pods->items[1];

或者你需要先解引用指针,然后直接访问数组:

item *an_item = (*pods).items[1]; // 我记不清楚这里是否需要括号了

Go语言有一个箭头运算符,但它用于写入通道:

ch := make(chan struct{}, 1)
ch <- struct{}{}

或者在将通道作为参数传递时指定通道的方向性:

func doStuff(ctx context.Context, ch chan<- struct{}) {
    // 这个函数只能向通道写入数据
}
func doMoreStuff(ctx context.Context, ch <-chan struct{}) {
    // 这个函数只能从通道读取数据
}

在访问对象的字段时,点运算符同时处理直接访问和间接访问。我在Go语言的页面上快速查看了一下,看看它们是否详细说明了这个设计决策,在关于访问结构体字段的Go语言教程页面上找到了这样一句话:

> 当我们有结构体指针p时,要访问结构体的字段X,我们可以写成(*p).X。然而,这种表示法很繁琐,所以语言允许我们直接写p.X,而不需要显式解引用。

所以简而言之,你可以写(*pods).Items并显式解引用指针,但这并不是必需的。在转向Go语言之前,我写过相当多的C代码,最初我认为我会更喜欢箭头运算符的明确性,因为我喜欢知道一个变量是指针还是非指针。然而,几年过去了,我不能说我怀念解引用、箭头运算符,更不用说多级间接访问了。99%的情况下,如果代码编写得很好,你都知道哪些是指针,哪些不是。

英文:

That's one of the differences between go and other (mostly older) languages in the C family. In some languages, you need to use a dedicated operator to access fields on objects that through indirection (ie pointers), in golang, the dot operator handles both.

In case someone stumbles across this question and doesn't know what this all means: If you had a pointer to a type like this in C:

typedef struct pod_list {
    item items[100]; // or something
} pod_list;

You would need to write the following to access the items array:

item * an_item = pods-&gt;items[1];

Or you'd need to dereference the pointer first, then access the array directly:

item *an_item = (*pods).items[1]; // can&#39;t remember off the top of my head if the brackets are needed here though

Golang has an arrow operator, but it's used to write to channels:

ch := make(chan struct{}, 1)
ch &lt;- struct{}{}

Or specify channel directionality when passing it as an argument:

func doStuff(ctx context.Context, ch chan&lt;- struct{}) {
    // this function can only write to the channel
}
func doMoreStuff(ctx context.Context, ch &lt;-chan struct{}) {
    // this function can only read from the channel
}

When it comes to accessing fields of objects, the . operator handles both direct and indirect access. I had a quick look on the golang pages to see if they elaborate on this design decision, and found this line on the golang tour page about accessing struct fields:

> To access the field X of a struct when we have the struct pointer p we could write (*p).X. However, that notation is cumbersome, so the language permits us instead to write just p.X, without the explicit dereference.

So the TL;DR is this: you could write (*pods).Items and explicitly dereference the pointer, but it's simply not needed. Having written a fair bit of C before moving to golang, I initially thought I'd prefer the explicit nature of the arrow operator, because I like knowing whether a variable is a pointer or not. Then again, after a few years, I can't say I miss the faffing around with dereferencing, arrow operators, let alone multiple levels of indirection. 99% of the time, if the code is well written, you know what is a pointer and what isn't anyway.

huangapple
  • 本文由 发表于 2022年4月29日 00:25:44
  • 转载请务必保留本文链接:https://go.coder-hub.com/72047202.html
匿名

发表评论

匿名网友

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

确定