Comments (4)
Great, It works.
erl:
-module(my_bank).
-behavior(gen_server).
-export([start/0,stop/0,new_account/1,deposit/2,withdraw/2,
init/1, handle_call/3, handle_cast/2, handle_info/2, terminate/2, code_change/3]).
%%====================================================================
%% server interface function
%%====================================================================
start()->
gen_server:start_link({local,?MODULE}, ?MODULE,[],[]).
stop()->
gen_server:call(?MODULE,stop).
new_account(Who)->
gen_server:call(?MODULE,{new,Who}).
deposit(Who,Amount)->
gen_server:call(?MODULE,{add,Who,Amount}).
withdraw(Who,Amount)->
gen_server:call(?MODULE,{remove,Who,Amount}).
%%====================================================================
%% gen_server callbacks
%%====================================================================
init([]) ->
Init=ets:new(?MODULE,[]),
io:format("init ~p ~p",[Init,?MODULE]),
{ok, Init}.
handle_call({new,Who}, _From, State) ->
Reply = case ets:lookup(State,Who) of
[]->ets:insert(State,{Who,0}),
{welcome,Who};
[_]->{exsits,Who,you_already_are_a_customer}
end,
{reply, Reply, State};
handle_call({add,Who,Amount},_From,State)->
Reply = case ets:lookup(State,Who) of
[]->not_a_customer;
[{Who,Balance}]->
NewBalance=Balance+Amount,
ets:insert(State,{Who,NewBalance}),
{thanks,Who,you_balance_is,NewBalance}
end,
{reply, Reply, State};
handle_call({remove,Who,Amount},_From,State)->
Reply = case ets:lookup(State,Who) of
[]->not_a_customer;
[{Who,Balance}] when Amount =< Balance ->
NewBalance=Balance-Amount,
ets:insert(State,{Who,NewBalance}),
{thanks,Who,your_balance_is,NewBalance};
[{Who,Balance}]->
{sorry,Who,you_only_have,Balance,in_the_bank}
end,
{reply, Reply, State}.
handle_cast(_Msg, State) ->
{noreply, State}.
handle_info(_Info, State) ->
{noreply, State}.
terminate(_Reason, _State) ->
io:format("is terminate.",[]),
ok.
code_change(_OldVsn, State, _Extra) ->
{ok, State}.
go
ret, _ := process.Call(etf.Tuple{"my_bank", "erlmain@locahost"}, etf.Tuple{etf.Atom("remove"), "tyt", 1})
from ergo.
Thanks for the question. I would recommend rewriting your erlang code to the OTP design patterns using gen_server behavior. The reason you don't receive the message because you didn't specify the correct pattern for that. Process.Call does the same as gen_server:call - a sending message has pattern {'$gen_call', From, Message}
.
There are 3 way to send a message:
- Process.Call (as i mentioned above) https://erlang.org/doc/man/gen_server.html#call-2
- Process.Cast - sends a message in manner gen_server:cast (https://erlang.org/doc/man/gen_server.html#cast-2)
- Process.Send - sends a simple message https://erlang.org/doc/man/erlang.html#send-2 (the same if you sending a message using
Pid ! Message
)
As of your erlang code... here is how it should look like (if you don't want to follow OTP design)
receive
{'$gen_call', _From, Message} -> % in case of using Process.Call
io:format("Got 'call' message: ~p", [Message]),
% you have to reply on it with correct answer like this https://github.coml/halturin/ergo/blob/master/gen_server.go#L81
% otherwise Process.Call fail with timeout
loop();
{'$gen_cast', Message} -> % in case of using Process.Cast
% you don't have to reply here
io:format("Got 'cast' message: ~p", [Message]),
loop();
OtherMessage -> % in case of using Process.Send
io:format("Got 'other' message: ~p", [OtherMessage]),
loop()
end.
from ergo.
Thanks a lot,I'll try it later
from ergo.
Let's leave it open for the others who get the same question.
from ergo.
Related Issues (20)
- How does ergo compare?
- GitHub actions fail on all platforms due to integration E2E tests HOT 1
- Unittest `TestDecodeFragment` occasionally fails
- i found rpc:call from erlang to ergo is not working HOT 2
- Ergo receives DIST packet along with the HANDSHAKE final packet. HOT 1
- Second CallRPC method is stuck when it is called against remote erlang node HOT 3
- Does Ergo have a plan to support OTP25/OTP26? HOT 1
- how to use simple_one_for_one under supervisor tree HOT 4
- Panic invalid memory address after reconnect a node HOT 6
- bug for deleteProcess HOT 3
- bug for (*process).UnregisterName HOT 2
- I doubt benchmarks be fair !!! HOT 3
- How to change Logger HOT 2
- gen.TcpHandle Question HOT 10
- When I close it frequently, there will be an unrecoverable panic. HOT 3
- Increase test coverage to 80% HOT 2
- node link problem? How to find HOT 8
- node link problem? HOT 2
- Support for Erlang/OTP 26 HOT 1
- Node.Stop () and node.Wait () will immediately end the process HOT 2
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from ergo.