当前位置:网站首页>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.
边栏推荐
猜你喜欢

LeetCode简单题之两个数组间的距离值
![[Android] Room - Alternative to SQLite](/img/52/0bc1c0a3173da6d39224ad8440a462.png)
[Android] Room - Alternative to SQLite

QML的使用

Ambiguous method call.both

JS function this context runtime syntax parentheses array IIFE timer delay self.backup context call apply

想从手工测试转岗自动化测试,需要学习哪些技能?

STM32 problem collection

Use of QML

How to develop a high-quality test case?

Multilingual settings of php website (IP address distinguishes domestic and foreign)
随机推荐
[Compilation principle] Lexical analysis program design principle and implementation
【Exception】The field file exceeds its maximum permitted size of 1048576 bytes.
C primer plus study notes - 8, structure
TCP详解(一)
[Godot][GDScript] 2D cave map randomly generated
[Android] Room - Alternative to SQLite
C primer plus学习笔记 —— 8、结构体
分布式锁以及实现方式三种
With 7 years of experience, how can functional test engineers improve their abilities step by step?
Good place to download jar packages
WebSocket Session is null
VS QT - ui does not display newly added members (controls) || code is silent
Several common errors when using MP
LeetCode每日一练 —— OR36 链表的回文结构
els 方块向右移动边界判断、向下加速
顺序表的实现
some of my own thoughts
LeetCode每日一练 —— 138. 复制带随机指针的链表
return in try-catch
【C语言】进制转换一般方法