-2

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

John Sibly
  • 22,782
  • 7
  • 63
  • 80
Sean Brown
  • 11
  • 1
  • 1

1 Answers1

5

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.

icza
  • 389,944
  • 63
  • 907
  • 827