当前位置:网站首页>Go-指针的那些事
Go-指针的那些事
2022-08-09 09:27:00 【草明】
<<Go 语言核心 36 讲>> 郝林 总结的
不可寻址的值:
- 常量的值。
- 基本类型值的字面量。
- 算术操作的结果值。
- 对各种字面量的索引表达式和切片表达式的结果值。不过有一个例外,对切片字面量的索引结果值却是可寻址的。
- 对字符串变量的索引表达式和切片表达式的结果值。
- 对字典变量的索引表达式的结果值。
- 函数字面量和方法字面量,以及对它们的调用表达式的结果值。
- 结构体字面量的字段值,也就是对结构体字面量的选择表达式的结果值。
- 类型转换表达式的结果值。
- 类型断言表达式的结果值。
- 接收表达式的结果值。
规律:
- 不可变的 - 寻址没有意义, 不能改
- 临时结果 - 例外, 切片字面量的索引结果值.
- 不安全的 - "不安全的"操作很可能会破坏程序的一致性,引发不可预知的错误,从而严重影响程序的功能和稳定性。
上代码, 自己品
package main
type Pet interface {
Name() string
Category() string
}
type Dog struct {
name string
}
func (self Dog) SetName(name string) {
self.name = name
}
func (self Dog) Name() string {
return self.name
}
func (self Dog) Category() string {
return "Dog"
}
func New(name string) Dog {
return Dog{
name}
}
func main() {
// 不会报错. 不需要指针类型调用 SetName
New("Max").SetName("Bear")
}
package main
type Pet interface {
Name() string
Category() string
}
type Dog struct {
name string
}
func (self *Dog) SetName(name string) {
self.name = name
}
func (self *Dog) Name() string {
return self.name
}
func (self *Dog) Category() string {
return "Dog"
}
func New(name string) Dog {
return Dog{
name}
}
func main() {
dog := New("Max")
dog.SetName("Bear") // 编译器会自动转换成 (&dog).SetName("Bear")
// 会报错, 因为 New("Max") 是临时结果, 不可以寻址, 无法获取指针
New("Max").SetName("Bear")
// cannot call pointer method on New("Max")
// cannot take the address of New("Max")
}
package main
import (
"fmt"
)
func main() {
map[string]int{
"the": 0, "word": 0, "counter": 0}["word"]++
map1 := map[string]int{
"the": 0, "word": 0, "counter": 0}
map1["word"]++
fmt.Println(map1["word"]) // 1
}
package main
import (
"fmt"
"unsafe"
)
type Dog struct {
name string
}
func (self *Dog) SetName(name string) {
self.name = name
}
func (self *Dog) Name() string {
return self.name
}
func (self *Dog) Category() string {
return "Dog"
}
func main() {
dog := Dog{
}
dog.SetName("Max")
dogP := &dog
dogPtr := uintptr(unsafe.Pointer(dogP))
namePtr := dogPtr + unsafe.Offsetof(dogP.name)
nameP := (*string)(unsafe.Pointer(namePtr))
fmt.Println(*nameP) // Max
namePtr = dogPtr + 64
unknownP := (*[]byte)(unsafe.Pointer(namePtr))
fmt.Println(*unknownP) // 内存泄漏, 获取到了某个地址指向的数据. 如果改了, 可能会导致不可知的问题
}
边栏推荐
- 软件测试面试思路技巧和方法分享,学到就是赚到
- 软件测试外包公司怎么样?有什么好处和坏处?为什么没人去?
- 第三方免费开放API 获取用户IP 并查询其地理位置
- 迭代
- Global 19 Google Satellite Map Free View Download
- JS报错-Uncaught TypeError: 'caller', 'callee', and 'arguments' properties may not be accessed on...
- 1.线程简介
- What are the basic concepts of performance testing?What knowledge do you need to master to perform performance testing?
- 本体开发日记05-努力理解SWRL(下)
- 电脑硬件基础知识科普
猜你喜欢

Another implementation of lateral view explode

.ts 音频文件转换成 .mp3 文件
软件测试外包公司怎么样?有什么好处和坏处?为什么没人去?
web测试之功能测试常用的方法有哪几种?有什么要点要注意?

【百日行动】炎炎夏日安全不松懈 消防培训“加满”安全知识“油”
银联最新测试工程师笔试题目,你能得多少分?

可以写进简历的软件测试项目实战经验(包含电商、银行、app等)

The div simulates the textarea text box, the height of the input text is adaptive, and the word count and limit are implemented
软件测试个人求职简历该怎么写,模板在这里

Domestic Google earth, terrain analysis seconds kill with the map software
随机推荐
【分布式事务】
MVCC multi-version concurrency control
本体开发日记03-排错进行时
白盒测试的概念、目的是什么?及主要方法有哪些?
软件测试外包公司怎么样?有什么好处和坏处?为什么没人去?
软件测试面试常见问题及答案(发散思维、接口、性能、概念、)
【面试体系知识点总结】---JVM
Cisco common basic configuration of common commands
MySQL event_single event_timed loop event
Ontology Development Diary 01-Jena Configuration Environment Variables
lateral view explode的另一种实现方式
可以写进简历的软件测试项目实战经验(包含电商、银行、app等)
本体开发日记05-努力理解SWRL(中)
Openwrt配置Aria2(Hg255d)
What are the basic concepts of performance testing?What knowledge do you need to master to perform performance testing?
字典
Ontology development diary 02 - simple sparql query
有返回值的函数
网络安全入门基础:IP地址
MySQL transaction isolation