For this reason, it's usually considered good form to pass values to functions instead of pointers, even if the values are larger. This allows the compiler to prove through escape analysis that the arguments don't need to be heap-allocated. In some cases pointers can be proven not to escape, but in a limited set of circumstances.
Having explicit pointers also resolves type system awkwardness between simple and complex data types. Consider Java, which is also a pass-by-value language, but has "reference" types, except for int, float, etc. So everything is a reference except this bag of simple data types, effectively creating a 2-tiered type system. Java has dealt with this by implementing boxing and unboxing rules for int/Integer and friends, which is fine, but adds complexity.
Finally, one of my personal favourite features about having a complex data type that is a value instead of a pointer or reference is that you can use structs and arrays as map keys.
> Having explicit pointers also resolves type system awkwardness between simple and complex data types.
That is it appears to be worth it to have explicit pointers for data structures (and the manipulation of those data structures) and probably more so with a concurrent language. That is when the programmer sees things that don't have pointers they can make some generally assumptions (the struct or whatever is generally immutable).
On the other handy, I have never really had a situation where I had to pass a strict that was really big (more then 1MB).
Most of the time, big data structures are actually just slices, which use pointers to point to the memory address of the underlying array (as far as I understand).
So having sich big struct is rather unusual. Maybe it is possible to do with strings, though it would depend on how are strings implemented in go (I would guess they are just slices as well).
Furthermore, having big strings 1<x<50 MB) is rather unusual unless the strings can also become really big (1GB in which case you should not probably pass-by-reference or even better, buffer the string because arbitrary length strings can easily cause ram problems).
I don't think there's really a rule of thumb here. Writing benchmarks, and observing the results is about the best you can do.
Given this, a more interesting question is "Why can't I define types that are implicitly always pointers?" I don't know the answer to that. My guess would be path-dependent design. As I understand it, even maps and chans were explicitly pointers for a long while in the early history of Go.
I've found that I can classify all structs in to two groups:
1) simple record values with all public fields; rare methods for convenience or specific polymorphism. 2) encapsulated machines with all private fields; only accessed through rich method interface.
Any blending of public and private fields has turned out to be a mistake in my experience. For record types, the implicit copying is valuable for passing data around. With encapsulated machines, I almost always want all usages to be a pointer. I wish I could define types like this:
type MyMachine *struct {
...private fields...
}
Which would basically compile like this: type machineState struct {
...public fields...
}
type MyMachine struct {
*machineState
}
One problem with this approximate macro-expansion is that you can't use "nil" for MyMachine, instead you need to use (MyMachine{}) as a zero value. I'd change that across the language too and just make nil a proper polymorphic literal zero value for any type.In other words, the default is pass by copy, but the fundamental idiomatic data structure is effectively pass by reference. This makes it way too easy to accidently pass parameters by reference when you didn't intend to or, even worse, try to manipulate the same array from multiple goroutines without proper locking.
This bit me hard in an early system I wrote in Go. The situation is marginally improved with the panic-on-multiple-thread-manipulating-slice feature. The only way to deal with it is to be paranoid about any code that passes slices, which weakens one of my favorite things about Go: you can look at the code, instantly understand what it is doing, and trust there is no magic.
Edit: to be clear, I think pointers should be included in Go. I would just prefer that slices be handled differently - perhaps by treating them as a special case, where the compiler passes the underlying array by copy unless explicitly told to pass by reference.
That's the biggest hole in Go's "share by communicating" story. In practice, Go channels are just a built-in queue object, with the same concurrency problems as queue objects in other languages. Rust has a borrow checker, so if you pass a reference through a queue, the compiler notices.
Gp strings are read-only, so slices of strings, although sharing data, are not sharing mutable data. That tends to make the problem at least manageable.
Buffering is only an option to improve performance. Programs should (if they are correct in a sense) be able to work with channels without buffering.
It depends on how you define a queue. That is in this case whether there is blocking or not. In Java this distinction is made clear with BlockingQueues and plain Queues.
A non buffering blocking queue in Java is a SynchronousQueue or casually known as a hand off queue or rendezvous queue. Unfortunately unlike Go these queues are painfully expensive in Java because Java doesn't have green threads as well I believe the queues use expensive locks (then again locks can be at times more efficient than spinning/sleeping the cpu aka ConcurrentLinkedQueue but in general they are expensive).
Going back to the ambiguity of what is a queue it gets even more complicated when talking about distributed queues/streams where you have acknowledgements, prefetching, round robin distribution etc (ie AMQP).
With Java (and lets assume Java now has Value types for argument sake) the default is immutable references but the language could easily provide other reference types (and in fact sort of does) that would allow pointer like behavior and value like behavior. And with immutable references the runtime may actually copy anyway for some types (I vaguely remember the JVM doing this somewhere).
The reason I don't mind pointers in Rust or C/C++ is that I expect to manage the memory because the languages doesn't do the memory management.
See golang escape analysis: http://www.agardner.me/golang/garbage/collection/gc/escape/a...
In C# where pointers are implicit, if you need to pass function parameters by reference you need to prefix them with the `ref` or `out` keywords depending on if they are supposed to be read-write or write-only. In Java, you need to wrap them with objects.
Then comes the arrays. In C#, they are references passed by value by default. Not references passed by reference, which still needs the `ref` keyword. So array content changes will be seen by the caller by default, but not full reassignments.
I think it can easily start to get confusing when you start to think about the details in what the language does under the hood, and all that only because of trying to steer clear from an innocent * character here and there. A character that also serves to inform and remind readers of the code what is going on, what the intent of the code is. And again, it's not as if other similar GC'ed languages can avoid this either. There always comes a point where the compiler must know. In Go's case I think the designers just chose the path to second guess less and just leave it to the user for less compiler magic.
+1 for the javascript breakouts every now and then.
My primary language (at the moment) doesn't make references the default and actually doesn't standardize allocation practice. This can be very confusing and will often lead to unexpected results unless you are aware enough of the nature of the language to avoid common pitfalls.
To me, this adds to the learning curve and is an unnecessary headache.
I'm not saying this means that explicit pointers are the "right" way of doing things...but just providing an example where a language without explicit pointers can result in odd behavior.
Also, I am not saying my primary language is bad...it is, after all, my primary language so I continue to use it quite extensively. Just trying to give some food for thought since you posed a good question.
That is I don't mind the explicitness of Rust because I guess I expect it just like C/C++ but I probably incorrectly expect it with Go lang. This is mostly because I come from a JVM background where I expect the VM to figure out what is more optimal and consequently also have very little experience using in/out parameters.
Implicit pointers means you have to often think extra when shuffling data structures around.
I understand the value of having pointers even in a GC but I'am actually more concerned with the resource I'm pointing to and its lifecycle than that is pointers. That is there should be different types of pointers depending on where the data is stored and how it is reclaimed (something Rust does nicely with generics).
I'm not trying to bash Go rather I must be missing something (I don't know the language that well).