当前位置:网站首页>addressable in Golang
addressable in Golang
2022-07-31 03:17:00 【Baijiafan Privacy Computing】
Let's take a look at the following sample program, the bottom test function, three lines are correct and one line is wrong, do you know why that line is wrong?
package jsimport ("fmt""testing")type Tester struct{}func (t Tester) Operation1() {fmt.Println("first")}func (t *Tester) Operation2() {fmt.Println("second")}func TestNewTester(t *testing.T) {Tester{}.Operation1() //correctTester{}.Operation2() //Errornew(Tester).Operation2() //correctnew(Tester).Operation1() //correct}There are several phenomena:
1) Functions defined on objects, such as this:
func (t Tester) Operation1() {fmt.Println("first")}It can be called directly in the following way
Tester{}.Operation1() //correctBut functions defined on pointers, such as:
func (t *Tester) Operation2() {fmt.Println("second")}It cannot be called in the following way
Tester{}.Operation2() //ErrorBut if the pointer defined by new(Tester) can be called
new(Tester).Operation2() //correctnew(Tester).Operation1() //correctIf I haven't tested it, I have always had the illusion that directly initialized objects cannot call functions, but from the above we can see that there are still many subtleties in the middle.
Then why?
It should be the object defined by Tester{} because there is no formal pointer reference, it is related to a state called addressable mentioned by the reflect library.This should not be addressable, so the pointer function cannot be called directly.
The official document is this
It mentions:
For an operandxof typeT, the address operation&xgenerates a pointer of type*Ttox. The operand must beaddressable, that is,either a variable, pointer indirection, or slice indexing operation; or a field selector of an addressable struct operand; or an array indexing operation of an addressable array. As an exception to the addressability requirement,xmay also be a (possibly parenthesized)composite literal. If the evaluationofxwould cause arun-time panic, then the evaluation of&xdoes too.
The bold part is added by me, which means that only variables, indirect pointers (such as a variable of an instance object directly calling the pointer function, actually the compiler turns the pointer first) or the index operation of the slice are calledis addressable.
边栏推荐
- SQL 面试用题(重点)
- C primer plus study notes - 8, structure
- [C language] Preprocessing operation
- Redis implements distributed locks
- Unity3D Button mouse hover enter and mouse hover exit button events
- Office automation case: how to automatically generate period data?
- TCP详解(二)
- CloudCompare&PCL 计算两个点云之间的重叠度
- The use of font compression artifact font-spider
- 【CocosCreator 3.5】CocosCreator get network status
猜你喜欢

Detailed explanation of TCP and UDP

Mysql 45讲学习笔记(二十三)MYSQL怎么保证数据不丢

What is a distributed lock?Three ways of implementing distributed lock
![[C language] Preprocessing operation](/img/69/0aef065ae4061edaf0d96b89846bf2.png)
[C language] Preprocessing operation

【C语言】预处理操作

一份高质量的测试用例如何养成?

Key Technologies of Interface Testing

Getting Started with CefSharp - winform

JS function this context runtime syntax parentheses array IIFE timer delay self.backup context call apply

6. Display comments and replies
随机推荐
SocialFi 何以成就 Web3 去中心化社交未来
[C language foundation] Solve C language error: expected ';', ',' or ')' before '&' token
什么是系统?
C#远程调试
With 7 years of experience, how can functional test engineers improve their abilities step by step?
TCP详解(二)
The simulation application of common mode inductance is here, full of dry goods for everyone
Discourse Custom Header Links
Moxa NPort device flaw could expose critical infrastructure to devastating attack
Problems that need to be solved in distributed system architecture
分布式系统架构需要解决的问题
Detailed explanation of TCP (1)
return in try-catch
Analysis summary - self-use
LeetCode每日一练 —— 138. 复制带随机指针的链表
els block to the right
What skills do I need to learn to move from manual testing to automated testing?
IDEA comment report red solution
[Godot][GDScript] 2D cave map randomly generated
MP使用时的几个常见报错