Erlang 是一种用于构建并发、分布式和容错系统的编程语言。以下是一些常用的命令和操作。
erl
# 在 Erlang Shell 中编译
c(module).
# 在命令行中编译
erlc module.erl
# 在 Erlang Shell 中运行
module:function().
# 从命令行运行
erl -noshell -s module function -s init stop
q().
-module(module_name).
-export([function_name/arity, ...]).
function_name(Args) ->
% Function body.
Result.
-export([function1/0, function2/1]).
% 单行注释
% 这是一个注释
VarName = Value. % 变量名必须以大写字母开头
Age = 25.
Name = "Alice".
atom. % 例子:atom, 'Atom with spaces'
123. % 整数
3.14. % 浮点数
true.
false.
"Hello, World!".
{ok, "Success"}.
[1, 2, 3].
[H|T] = [1, 2, 3]. % H = 1, T = [2, 3]
#{key1 => value1, key2 => value2}.
if
Condition1 -> Expression1;
Condition2 -> Expression2;
true -> DefaultExpression
end.
case Expression of
Pattern1 -> Expression1;
Pattern2 -> Expression2;
_ -> DefaultExpression
end.
% 无参函数
my_function() ->
ok.
% 有参函数
add(A, B) ->
A + B.
% 生成 1 到 10 的列表
[ X || X <- lists:seq(1, 10)].
% 生成 1 到 10 中的偶数
[ X || X <- lists:seq(1, 10), X rem 2 == 0].
spawn(Module, Function, Args).
% 示例
Pid = spawn(fun() -> io:format("Hello from process~n") end).
Pid ! Message.
% 示例
Pid ! {hello, self()}.
receive
Pattern1 -> Expression1;
Pattern2 -> Expression2;
after Timeout -> TimeoutExpression
end.
{ok, Value} = {ok, 42}.
lists:append(List1, List2).
lists:map(Function, List).
lists:filter(Function, List).
lists:foldl(Function, Acc, List).
element(N, Tuple).
setelement(N, Tuple, Value).
tuple_size(Tuple).
string:len(String).
string:concat(String1, String2).
string:tokens(String, Delimiters).
file:read_file(Filename).
file:write_file(Filename, Data).
file:delete(Filename).
lists:map(fun(X) -> X * 2 end, [1, 2, 3]).
lists:filter(fun(X) -> X rem 2 == 0 end, [1, 2, 3, 4]).
string:len("Hello").
string:upper("hello").
{ok, File} = file:open("test.txt", [write]).
file:write(File, "Hello, file!").
file:close(File).
-module(server).
-export([start/0, loop/0]).
start() ->
spawn(fun loop/0).
loop() ->
receive
{echo, Msg} ->
io:format("Echo: ~p~n", [Msg]),
loop();
stop ->
io:format("Server stopping~n"),
ok;
_ ->
io:format("Unknown message~n"),
loop()
end.
Pid = spawn(Module, Function, Args).
Pid ! Message.
receive
Pattern1 -> Actions1;
Pattern2 -> Actions2;
...
end.
link(Pid).
unlink(Pid).
MonitorRef = erlang:monitor(process, Pid).
erlang:demonitor(MonitorRef).
try Expression of
Pattern -> Result
catch
Class:Reason -> Handler
end.
throw
error
exit
try Expression of
Pattern -> Result
catch
Type:Reason -> ErrorHandlingExpression
end.
erl -name nodename@hostname -setcookie Cookie
net_adm:ping(Node).
{remote_process, 'remote_node@host'} ! Message.
-module(my_gen_server).
-behaviour(gen_server).
-export([start_link/0, init/1, handle_call/3, handle_cast/2, handle_info/2, terminate/2, code_change/3]).
start_link() ->
gen_server:start_link({local, ?MODULE}, ?MODULE, [], []).
init([]) ->
{ok, #state{}}.
handle_call(Request, From, State) ->
{reply, Reply, State}.
handle_cast(Msg, State) ->
{noreply, State}.
handle_info(Info, State) ->
{noreply, State}.
terminate(Reason, State) ->
ok.
code_change(OldVsn, State, Extra) ->
{ok, State}.
gen_server:start_link({local, Name}, Module, Args, Options).
gen_server:call(ServerRef, Request).
gen_server:cast(ServerRef, Msg).
-module(module_name_tests).
-include_lib("eunit/include/eunit.hrl").
simple_test() ->
?assertEqual(Expected, Actual).
complex_test_() ->
[
{"Test case 1", ?_assertEqual(Expected1, Actual1)},
{"Test case 2", ?_assertEqual(Expected2, Actual2)}
].
# 在命令行中运行
erl -eval "eunit:test(module_name)" -s init stop