当前位置:网站首页>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 injection Less46 (injection after order by + rand() Boolean blind injection)

大小端模式

【编译原理】递归下降语法分析设计原理与实现

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

4. Sensitive word filtering (prefix tree)

递归查询单表-单表树结构-(自用)

11. Redis implements follow, unfollow, and follow and follower lists

C# remote debugging
![[C language] Preprocessing operation](/img/69/0aef065ae4061edaf0d96b89846bf2.png)
[C language] Preprocessing operation

7年经验,功能测试工程师该如何一步步提升自己的能力呢?
随机推荐
[C language] Preprocessing operation
[Godot][GDScript] 二维洞穴地图随机生成
LeetCode simple problem to find the subsequence of length K with the largest sum
【C语言】预处理操作
TCP和UDP详解
数据库实现分布式锁
选好冒烟测试用例,为进入QA的制品包把好第一道关
冒泡排序、选择排序、直接插入排序、二分法查找
The simulation application of common mode inductance is here, full of dry goods for everyone
C primer plus study notes - 8, structure
Day32 LeetCode
SQL injection Less54 (limited number of SQL injection + union injection)
SQL injection Less47 (error injection) and Less49 (time blind injection)
4. Sensitive word filtering (prefix tree)
分布式与集群是什么 ? 区别是什么?
11. Redis implements follow, unfollow, and follow and follower lists
Mysql 45讲学习笔记(二十五)MYSQL保证高可用
web容器及IIS --- 中间件渗透方法1
Detailed explanation of TCP (3)
Day32 LeetCode