note4cs/技术/Go/八股.md
2025-03-19 14:36:09 +08:00

7 lines
630 B
Markdown
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

## Go语言中如何访问私有成员
在Go语言中以小写字母开头的标识符是私有成员私有成员字段、方法、函数等遵循语言的可见性规则仅在定义它的包内可见包外无法访问这些私有成员。如果想要访问私有成员主要包括以下三种方式
- 在同一个包内,可以直接访问**小写字母**开头的私有成员。
- 在其他包中,无法直接访问私有成员,但可以通过公开的**接口**来间接访问私有成员。
- 使用**反射**来绕过Go语言的封装机制访问和修改私有字段。(**不建议使用**)
---