当前位置:网站首页>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.
边栏推荐
- Graphical lower_bound & upper_bound
- Good place to download jar packages
- Database implements distributed locks
- What skills do I need to learn to move from manual testing to automated testing?
- IIR滤波器和FIR滤波器
- LeetCode simple problem to find the subsequence of length K with the largest sum
- False positives and false negatives in testing are equally worthy of repeated corrections
- 【动态规划】连续子数组的最大和
- MultipartFile file upload
- 【CocosCreator 3.5】CocosCreator get network status
猜你喜欢

【C语言】求两个整数m和n的最大公因数和最小公倍数之和一般方法,经典解法

How to develop a high-quality test case?

品牌广告投放平台的中台化应用与实践

立足本土,链接全球 | 施耐德电气“工业SI同盟”携手伙伴共赴未来工业

Is interprofessional examination difficult?Low success rate of "going ashore"?Please accept this practical guide!

TCP详解(三)

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

6. Display comments and replies

Addition and Subtraction of Scores in LeetCode Medium Questions

华为分布式存储FusionStorage知识点总结【面试篇】
随机推荐
return in try-catch
Select the smoke test case, and make the first pass for the product package entering QA
【Exception】The field file exceeds its maximum permitted size of 1048576 bytes.
华为分布式存储FusionStorage知识点总结【面试篇】
VS QT - ui does not display newly added members (controls) || code is silent
【编译原理】词法分析程序设计原理与实现
IIR滤波器和FIR滤波器
Number 16, top posts
STM32问题合集
SonarQube的BUG定义
web容器及IIS --- 中间件渗透方法1
3.5 】 【 Cocos Creator slow operating system to stop all animations
Mysql 45讲学习笔记(二十三)MYSQL怎么保证数据不丢
SocialFi 何以成就 Web3 去中心化社交未来
Chapter 9 SVM实践
SQL 面试用题(重点)
什么是分布式锁?实现分布式锁的三种方式
点云DBSCAN聚类(MATLAB,非内置函数)
12 Disk related commands
els 方块向右移动边界判断、向下加速