3702, I think you need to learn some fundamental concepts to better 
understand the problem at hand.


Processors have more than one core. That means we have several threads 
running simultaneously. To let more than one thread access the same memory 
region is a bad idea. If your program simply crashes consider yourself a 
lucky guy. There are other subtle problems that can be dangerous and 
difficult to spot.


So, the programmer must find a way to avoid those simultaneous access. A 
popular way is by using some synchronization object, i.e. a mutex. Before 
any access to the shared state, a thread must lock the mutex, releasing it 
when the access is done. For small programs, it works. However, as your 
program grows, that approach can get complicated in insidious ways. 


Go offers mutex, as several other languages do, but it offers a much better 
device: channels. There are dozens of ways to use channels, but I will give 
you just an example.


Divide your program so that only one goroutine can access the memory area. 
Let’s call this goroutine the “data owner”. That way you eliminate the 
shared state problem at once, because there will be no shared state, at all.


When some other goroutine needs to read or change that state, it ASKs the 
data owner to do the access on its behalf. How? By sending a request thru a 
channel, that’s how. The data owner process those requests, performing the 
required actions.


Learn to use channels, 3702. After a little practice, your programs will 
run fast and safely. As an additional benefit the architecture will be 
solid, easy to change when the need arises.

On Thursday, July 27, 2017 at 7:28:39 AM UTC-3, 3702...@qq.com wrote:
>
> o yea,thanks.it's a good way to improve the performance of my program.but 
> will it solve the problem,crashing?
>
> 在 2017年7月27日星期四 UTC+8上午12:26:36,JuciÊ Andrade写道:
>>
>> I mean bytes.Buffer.
>>
>> On Wednesday, July 26, 2017 at 8:08:13 AM UTC-3, JuciÊ Andrade wrote:
>>>
>>> Dear friend 3702, while you are at it, please change that string 
>>> appending strategy. Use a bufio.Buffer instead. It's way faster.
>>>
>>>

-- 
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