当前位置:网站首页>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() //correct
But functions defined on pointers, such as:
func (t *Tester) Operation2() {fmt.Println("second")}
It cannot be called in the following way
Tester{}.Operation2() //Error
But if the pointer defined by new(Tester) can be called
new(Tester).Operation2() //correctnew(Tester).Operation1() //correct
If 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 operandx
of typeT
, the address operation&x
generates a pointer of type*T
tox
. 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,x
may also be a (possibly parenthesized)composite literal. If the evaluationofx
would cause arun-time panic, then the evaluation of&x
does 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.
边栏推荐
猜你喜欢
Mycat's master-slave relationship, vertical sub-database, horizontal sub-table, and detailed configuration of mycat fragmented table query (mysql5.7 series)
Problems that need to be solved in distributed system architecture
MP使用时的几个常见报错
什么是系统?
11. Redis implements follow, unfollow, and follow and follower lists
Detailed explanation of TCP (3)
Moxa NPort device flaw could expose critical infrastructure to devastating attack
Use of QML
Project (5) - Small target detection tph-yolov5
With 7 years of experience, how can functional test engineers improve their abilities step by step?
随机推荐
递归查询单表-单表树结构-(自用)
The simulation application of common mode inductance is here, full of dry goods for everyone
大小端模式
Moxa NPort 设备缺陷可能使关键基础设施遭受破坏性攻击
【编译原理】词法分析程序设计原理与实现
Day32 LeetCode
endian mode
Difference between unallocated blocks and unused blocks in database files
2022牛客多校联赛第四场 题解
TCP和UDP详解
With 7 years of experience, how can functional test engineers improve their abilities step by step?
PMP WeChat group daily exercises
Ambiguous method call.both
Detailed explanation of TCP (3)
[Godot][GDScript] 二维洞穴地图随机生成
SocialFi 何以成就 Web3 去中心化社交未来
【CocosCreator 3.5】CocosCreator 获取网络状态
Installation of mysql5.7.37 under CentOS7 [perfect solution]
【编译原理】递归下降语法分析设计原理与实现
CloudCompare&PCL 计算两个点云之间的重叠度