在Go中增加一个指针 [英] incrementing a pointer in Go

查看:301
本文介绍了在Go中增加一个指针的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

任何人都可以告诉我如何通过Go中的字符串增加指针?我已经尝试过ptr + = 1,就像在C中一样,但它表示类型* string和int不兼容。 Thanks

解决方案

Go 常见问题:为什么没有指针算术?


安全性。如果没有指针算术,可以创建一种永远不会导致错误地成功的非法地址的语言。编译器和硬件技术已经发展到使用数组索引的循环可以像使用指针运算的循环一样高效。另外,缺少指针算术可以简化垃圾回收器的实现。


所以答案是否定的,你不能增加一个指向Go的指针。


could anyone tell me how to increment a pointer through a string in Go? I've tried ptr += 1, like in C, but it says that the types *string and int are incompatible. Thanks

解决方案

Go FAQ: Why is there no pointer arithmetic?

Safety. Without pointer arithmetic it's possible to create a language that can never derive an illegal address that succeeds incorrectly. Compiler and hardware technology have advanced to the point where a loop using array indices can be as efficient as a loop using pointer arithmetic. Also, the lack of pointer arithmetic can simplify the implementation of the garbage collector.

So the answer is no, you can't increment a pointer in Go.

这篇关于在Go中增加一个指针的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆