1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

paiza.ioでelixirAdvent Calendar 2022

Day 10

paiza.ioでelixir その10

Last updated at Posted at 2022-10-25

概要

paiza.ioでelixirやってみた。
genserverとloggerやってみた。

参考にしたページ

サンプルコード

defmodule MyServer1 do
	use GenServer
	require Logger
	def start do
		GenServer.start(MyServer1, nil)
	end
	def put(pid, key, value) do
	    Logger.info("put")
		GenServer.cast(pid, {:put, key, value})
	end
	def get(pid, key) do
		Logger.info("get")
		GenServer.call(pid, {:get, key})
	end
	def init(_) do
	    Logger.info("init")
		{:ok, %{}}
	end
	def handle_cast({:put, key, value}, state) do
		{:noreply, Map.put(state, key, value)}
	end
	def handle_call({:get, key}, _, state) do
		{:reply, Map.get(state, key), state}
	end
end

{:ok, pid} = MyServer1.start()

MyServer1.put(pid, :my_key, :taro)
|> IO.puts
MyServer1.get(pid, :my_key)
|> IO.puts
MyServer1.put(pid, :my_key, :jiro)
|> IO.puts
MyServer1.get(pid, :my_key)
|> IO.puts
GenServer.call(pid, {:get, :my_key})
|> IO.puts


実行結果


02:05:34.481 [info]  init

02:05:34.483 [info]  put
ok

02:05:34.484 [info]  get
taro
ok

02:05:34.484 [info]  put

02:05:34.484 [info]  get
jiro
jiro

成果物

以上。

1
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?