Filling in the gen_server Template

Making an OTP gen_server is largely a matter of filling in a boilerplate template with some code of your own. The following is an example; the individual sections of the gen_server were listed in the previous section. The gen_server template itself is built into Emacs, but if you’re not using Emacs, you can find the entire template in The Generic Server Template.

I’ve filled in the template to make a bank module called my_bank. This code is derived from the template. I’ve removed all the comments from the template so you can clearly see the structure of the code.

my_bank.erl
 
-module​(my_bank).
 
 
-behaviour​(gen_server).
 
-export​([start/0]).
 
%% gen_server callbacks
 
-export​([init/1, handle_call/3, ...

Get Programming Erlang, 2nd Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.