当前位置:网站首页>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.
边栏推荐
- Modbus on AT32 MCUs
- 分布式与集群是什么 ? 区别是什么?
- Redis implements distributed locks
- Is interprofessional examination difficult?Low success rate of "going ashore"?Please accept this practical guide!
- Discussion on Service Commitment of Class Objects under Multithreading
- Mysql 45讲学习笔记(二十五)MYSQL保证高可用
- 识Flutter 基本组件之showTimePicker 方法
- What is SQALE
- 数据库文件中的未分配的块和未使用的块的区别
- 遗留系统的自动化策略
猜你喜欢

LeetCode每日一练 —— OR36 链表的回文结构

What skills do I need to learn to move from manual testing to automated testing?

Moxa NPort device flaw could expose critical infrastructure to devastating attack

Crypto Firms Offer Offer To Theft Hackers: Keep A Little, Give The Rest

Addition and Subtraction of Scores in LeetCode Medium Questions

接口测试关键技术

STM32 problem collection

SIP协议标准和实现机制

LeetCode简单题之找到和最大的长度为 K 的子序列

Recursive query single table - single table tree structure - (self-use)
随机推荐
接口测试关键技术
2022牛客多校联赛第四场 题解
Mysql 45讲学习笔记(二十五)MYSQL保证高可用
SIP Protocol Standard and Implementation Mechanism
【C语言】三子棋(经典解法+一览图)
[Godot][GDScript] 二维洞穴地图随机生成
【C语言】进制转换一般方法
IDEA comment report red solution
IIR滤波器和FIR滤波器
编译Hudi
12 Disk related commands
华为分布式存储FusionStorage知识点总结【面试篇】
C primer plus study notes - 8, structure
What is distributed and clustered?What is the difference?
els 方块向左移动条件判断
SQL injection Less54 (limited number of SQL injection + union injection)
Compile Hudi
[Compilation principle] Lexical analysis program design principle and implementation
Office automation case: how to automatically generate period data?
Detailed explanation of TCP (1)