Pointers are useful for several reasons. Pointers allow you to control the layout of the memory (affects the efficiency of the processor cache). In Go, we can define a structure in which all members are in contiguous memory:
type Point struct { x, y int } type LineSegment struct { source, destination Point }
In this case, Point structures are embedded in the LineSegment structure. But you cannot always directly insert data. If you want to support structures such as binary trees or a linked list, then you need to maintain some kind of pointer.
type TreeNode { value int left *TreeNode right *TreeNode }
Java, Python, etc. do not have this problem, because they do not allow the insertion of composite types, so there is no need to syntactically distinguish between nesting and guidance.
Problems with Swift / C # Structures Solved with Go Pointers
A possible alternative to doing this is to distinguish between struct and class like C # and Swift. But this has limitations. Although you can usually indicate that a function takes a structure as an inout to avoid copying the structure, it does not allow you to store references (pointers) to structures. This means that you can never consider a structure as a reference type when you find it useful, for example, to create a pool allocator (see below).
Custom memory block
Using pointers, you can also create your own pool allocator (this is very simplified with a lot of checks removed to just show the principle):
type TreeNode { value int left *TreeNode right *TreeNode nextFreeNode *TreeNode; // For memory allocation } var pool [1024]TreeNode var firstFreeNode *TreeNode = &pool[0] func poolAlloc() *TreeNode { node := firstFreeNode firstFreeNode = firstFreeNode.nextFreeNode return node } func freeNode(node *TreeNode) { node.nextFreeNode = firstFreeNode firstFreeNode = node }
Change two values
Pointers also allow you to implement swap . This replaces the values of two variables:
func swap(a *int, b *int) { temp := *a *a = *b *b = temp }
Conclusion
Java has never been able to completely replace C ++ for system programming in places like Google, partly because performance cannot be tuned to the same degree due to the inability to control layout and memory usage (cache misses affect performance significantly). Go aims to replace C ++ in many areas and therefore must support pointers.