You can not.
If the runtime cannot allocate memory for adding, it may be impossible to restore or to contact the user "Resource is temporarily unavailable." For example, the GC may need to be allocated for cleaning, or the scheduler may try to allocate a new thread. Since there is no way to strictly control allocations in a Go program, there is no way to gracefully manage memory exhaustion.
All OOM conditions complete the Go program.
source share