filmov
tv
Intro to GenServer | Elixir | W3Codify

Показать описание
GenServer
Jump into the Elixir GenServer module. In this episode, we’ll get started using GenServer to create a simple shopping list module that can we can use to add, remove, and view our groceries.
Elixir comes with a set of libraries known as OTP.
One of those libraries is the GenServer module, which provides a standard interface to help handle concurrency.
In this episode, we’ll get started with the basics of GenServer by using it to help us manage a shopping list.
Let’s get started.
Here we have an empty module called ‘ShoppingList’.
First, we’ll add the use macro to bring in the GenServer module.
Let’s create a function named start_link, which we’ll use to start our shopping list process.
And inside it, we’ll call the GenServer function of the same name, start_link.
We’ll use __MODULE__ to return our ShoppingList module, and then we’ll pass in any initial state we want to start with. We’ll use an empty list.
Let’s define it.
We’ll call it ‘list’ since it represents our shopping list.
‘init’, along with most other GenServer functions, has a specific format it needs to return.
We’ll keep ours simple and return a 2-element tuple {:ok, 'list'} to get our ShoppingList started.
#elixir #genserver #erlang #elixirstrings #application #function #phoenix #webapplications #webdevelopment #software
Jump into the Elixir GenServer module. In this episode, we’ll get started using GenServer to create a simple shopping list module that can we can use to add, remove, and view our groceries.
Elixir comes with a set of libraries known as OTP.
One of those libraries is the GenServer module, which provides a standard interface to help handle concurrency.
In this episode, we’ll get started with the basics of GenServer by using it to help us manage a shopping list.
Let’s get started.
Here we have an empty module called ‘ShoppingList’.
First, we’ll add the use macro to bring in the GenServer module.
Let’s create a function named start_link, which we’ll use to start our shopping list process.
And inside it, we’ll call the GenServer function of the same name, start_link.
We’ll use __MODULE__ to return our ShoppingList module, and then we’ll pass in any initial state we want to start with. We’ll use an empty list.
Let’s define it.
We’ll call it ‘list’ since it represents our shopping list.
‘init’, along with most other GenServer functions, has a specific format it needs to return.
We’ll keep ours simple and return a 2-element tuple {:ok, 'list'} to get our ShoppingList started.
#elixir #genserver #erlang #elixirstrings #application #function #phoenix #webapplications #webdevelopment #software