Skip to content

Commit

Permalink
docs: interface
Browse files Browse the repository at this point in the history
  • Loading branch information
duanbiaowu authored Mar 3, 2024
1 parent ed8f3be commit 4414a57
Showing 1 changed file with 12 additions and 0 deletions.
12 changes: 12 additions & 0 deletions content/introduction/interface.md
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,18 @@ func main() {
*/
```

# 编译检测规则

编译器会对接口方法的 `接收者参数` 进行检查,具体来说:

- 接收者形参为普通变量类型
- 实参为普通变量类型,编译正常
- 实参为指针变量类型,编译正常
- 接收者形参为指针变量类型
- **实参为普通变量类型,编译报错**,因为编译器不会无缘无故创建一个新的指针,退一步说,即使编译器可以创建指针指向变量,但是因为 Go 语言传递参数方式为 `值传递`,此时新指针指向的变量,并不是调用该方法的变量,而是经过复制传递的参数变量。
- 实参为指针变量类型,编译正常


# reference

1. [Go 圣经](https://book.douban.com/subject/27044219)

0 comments on commit 4414a57

Please sign in to comment.