In C, we can move pointer like so:
int main()
{
int array[4] = { 1, 2, 3, 4};
int *q;
q = array;
printf("%d\n", q[0]); /* 1 */
q += 3;
printf("%d\n", q[0]); /* 4 */
}
Is there a way to do something like this in Golang?
In C, we can move pointer like so:
int main()
{
int array[4] = { 1, 2, 3, 4};
int *q;
q = array;
printf("%d\n", q[0]); /* 1 */
q += 3;
printf("%d\n", q[0]); /* 4 */
}
Is there a way to do something like this in Golang?
Golang does not support pointer arithmetic since that is a source of vulnerabilities.
References: https://tour.golang.org/moretypes/1
Golang does not support pointer arithmetic
in order to increase the safety during development.
For example, using Go slices,
package main
import "fmt"
func main() {
array := [4]int{1, 2, 3, 4}
slice := array[:]
fmt.Printf("%d\n", slice[0]) /* 1 */
slice = slice[3:]
fmt.Printf("%d\n", slice[0]) /* 4 */
}
Playground: https://play.golang.org/p/82pXFkHLSfd
Output:
1
4