What Cris said is one reason. That is, if you need the function being 
called to be able to change the value, then use a pointer. If you pass a 
value instead of a pointer, then changes made by the function being called 
are not visible to the caller. (The situation is more complicated, but that 
is the gist.)

If you do not need the function to be able to change the value, then 
default to using pass by value. In general, pass by value should be your 
default mode. If a pointer is used, there should always be a clear reason. 
Sometimes pointers are used to pass a very large structure, to avoid 
copying. But I would only do that if the copying was demonstrated to be a 
performance issue.  

There is another reason to use pointers. Some objects must never be 
"copied". Passing a struct by value causes it to be copied, and the copy 
sent to the function being called. One example is sync.Mutex 
<https://golang.org/pkg/sync/#Mutex>. The documentation for that states 
that "a Mutex must not be copied after first use." So a sync.Mutex 
<https://golang.org/pkg/sync/#Mutex> must always be passed by pointer. And 
any struct that contains a non-pointer sync.Mutex would also need to be 
passed by pointer. 

Good Luck.

On Tuesday, January 1, 2019 at 6:34:34 AM UTC-5, 伊藤和也 wrote:
>
> What are the reasonable reasons to use pointers? Are pointers neseccary?
>

-- 
You received this message because you are subscribed to the Google Groups 
"golang-nuts" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to golang-nuts+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to