当前位置:网站首页>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.
边栏推荐
- STM32问题合集
- 2022 Nioke Multi-School League Game 4 Solution
- [Godot][GDScript] 2D cave map randomly generated
- Moxa NPort 设备缺陷可能使关键基础设施遭受破坏性攻击
- Mycat's master-slave relationship, vertical sub-database, horizontal sub-table, and detailed configuration of mycat fragmented table query (mysql5.7 series)
- 大小端模式
- Distributed locks and three implementation methods
- SQL injection Less54 (limited number of SQL injection + union injection)
- 11. Redis implements follow, unfollow, and follow and follower lists
- 7年经验,功能测试工程师该如何一步步提升自己的能力呢?
猜你喜欢
Redis实现分布式锁
学习DAVID数据库(1)
Is interprofessional examination difficult?Low success rate of "going ashore"?Please accept this practical guide!
【异常】The field file exceeds its maximum permitted size of 1048576 bytes.
Compile Hudi
分布式系统架构需要解决的问题
What skills do I need to learn to move from manual testing to automated testing?
【C语言】求两个整数m和n的最大公因数和最小公倍数之和一般方法,经典解法
10 Permission introduction
STM32 problem collection
随机推荐
Detailed explanation of TCP (1)
下载jar包的好地方
冒泡排序、选择排序、直接插入排序、二分法查找
[C language foundation] Solve C language error: expected ';', ',' or ')' before '&' token
顺序表的实现
Automation strategies for legacy systems
IDEA comment report red solution
Number 16, top posts
Several common errors when using MP
Mycat's master-slave relationship, vertical sub-database, horizontal sub-table, and detailed configuration of mycat fragmented table query (mysql5.7 series)
4. Sensitive word filtering (prefix tree)
WebSocket Session is null
endian mode
原子操作 CAS
SQL injection Less47 (error injection) and Less49 (time blind injection)
YOLOV5 study notes (2) - environment installation + operation + training
编译Hudi
【HCIP】ISIS
SIP Protocol Standard and Implementation Mechanism
MultipartFile文件上传