当前位置:网站首页>Golang中的addressable
Golang中的addressable
2022-07-31 03:06:00 【百家饭隐私计算】
我们来看下面一个示例程序,最下面的测试函数,其中有三行是正确的,一行是错误的,你知道为什么那一行是错的么?
package js
import (
"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() //正确
Tester{}.Operation2() //错误
new(Tester).Operation2() //正确
new(Tester).Operation1() //正确
}
有这么几个现象:
1)定义在对象上的函数,比如这个:
func (t Tester) Operation1() {
fmt.Println("first")
}
是直接可以通过下面的方式调用的
Tester{}.Operation1() //正确
但是定义在指针上的函数,比如:
func (t *Tester) Operation2() {
fmt.Println("second")
}
是不可以使用下面的方式调用的
Tester{}.Operation2() //错误
但是如果用new(Tester)定义出来的指针则都可以调用
new(Tester).Operation2() //正确
new(Tester).Operation1() //正确
如果没有测试过,我也一直有这么一种错觉,就是直接初始化的对象不能调用函数,但从上面我们就可以看出,中间还是有很多细微的地方。
那这是为什么呢?
应该是Tester{}这种方式定义出来的对象因为没有正式的指针引用过,是和reflect库提到过的一种叫addressable的状态有关。这种应该是还没有addressable,所以不能直接调用指针函数。
官方文档是这个
里面提到:
For an operandx
of typeT
, the address operation&x
generates a pointer of type*T
tox
. The operand must be addressable, 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 evaluation ofx
would cause a run-time panic, then the evaluation of&x
does too.
加粗的部分是我加的,意思是只有变量,间接指针(比如一个实例对象的变量直接调指针函数,实际是编译器先转了一遍指针)或切片的index操作,才称之为addressable。
边栏推荐
- Getting Started with CefSharp - winform
- Moxa NPort device flaw could expose critical infrastructure to devastating attack
- False positives and false negatives in testing are equally worthy of repeated corrections
- Project (5) - Small target detection tph-yolov5
- 【C语言】预处理操作
- QML的使用
- 8. Unified exception handling (controller notifies @ControllerAdvice global configuration class, @ExceptionHandler handles exceptions uniformly)
- 10. Redis implements likes (Set) and obtains the total number of likes
- Map.Entry理解和应用
- Mysql 45讲学习笔记(二十三)MYSQL怎么保证数据不丢
猜你喜欢
Mycat's master-slave relationship, vertical sub-database, horizontal sub-table, and detailed configuration of mycat fragmented table query (mysql5.7 series)
【异常】The field file exceeds its maximum permitted size of 1048576 bytes.
CorelDRAW2022 streamlined Asia Pacific new features in detail
【Android】Room —— SQLite的替代品
图解lower_bound&upper_bound
QML的使用
【Exception】The field file exceeds its maximum permitted size of 1048576 bytes.
php 网站的多语言设置(IP地址区分国内国外)
JS 函数 this上下文 运行时点语法 圆括号 数组 IIFE 定时器 延时器 self.备份上下文 call apply
Moxa NPort device flaw could expose critical infrastructure to devastating attack
随机推荐
Analysis summary - self-use
【C语言】进制转换一般方法
Mysql 45讲学习笔记(二十三)MYSQL怎么保证数据不丢
10、Redis实现点赞(Set)和获取总点赞数
[Compilation principle] Design principle and implementation of recursive descent parsing
SQALE 是什么
TCP详解(二)
Is interprofessional examination difficult?Low success rate of "going ashore"?Please accept this practical guide!
【Android】Room —— SQLite的替代品
【Exception】The field file exceeds its maximum permitted size of 1048576 bytes.
想从手工测试转岗自动化测试,需要学习哪些技能?
Problems that need to be solved in distributed system architecture
Project (5) - Small target detection tph-yolov5
MultipartFile file upload
TCP/IP four-layer model
MP使用时的几个常见报错
TCP详解(三)
Detailed explanation of TCP (3)
Why is String immutable?
What is a distributed lock?Three ways of implementing distributed lock