Erlang 是一种用于构建并发、分布式和容错系统的编程语言。以下是一些常用的命令和操作。

入门

启动 Erlang Shell

1
erl

编译代码

1
2
3
4
# 在 Erlang Shell 中编译
c(module).
# 在命令行中编译
erlc module.erl

运行代码

1
2
3
4
# 在 Erlang Shell 中运行
module:function().
# 从命令行运行
erl -noshell -s module function -s init stop

退出 Erlang Shell

1
q().

代码结构

模块定义

1
2
3
4
5
6
-module(module_name).
-export([function_name/arity, ...]).

function_name(Args) ->
% Function body.
Result.

导出函数

1
-export([function1/0, function2/1]).

注释

1
2
% 单行注释
% 这是一个注释

变量

1
2
3
VarName = Value. % 变量名必须以大写字母开头
Age = 25.
Name = "Alice".

数据类型

原子

1
atom.       % 例子:atom, 'Atom with spaces'

数字

1
2
123.        % 整数
3.14. % 浮点数

布尔值

1
2
true.
false.

字符串

1
"Hello, World!".

元组

1
{ok, "Success"}.

列表

1
2
[1, 2, 3].
[H|T] = [1, 2, 3]. % H = 1, T = [2, 3]

字典 (Map)

1
#{key1 => value1, key2 => value2}.

控制结构

条件语句

1
2
3
4
5
if
Condition1 -> Expression1;
Condition2 -> Expression2;
true -> DefaultExpression
end.

case 表达式

1
2
3
4
5
case Expression of
Pattern1 -> Expression1;
Pattern2 -> Expression2;
_ -> DefaultExpression
end.

函数定义

1
2
3
4
5
6
7
% 无参函数
my_function() ->
ok.

% 有参函数
add(A, B) ->
A + B.

列表操作

列表生成

1
2
3
4
5
% 生成 1 到 10 的列表
[ X || X <- lists:seq(1, 10)].

% 生成 1 到 10 中的偶数
[ X || X <- lists:seq(1, 10), X rem 2 == 0].

并发

启动进程

1
2
3
4
spawn(Module, Function, Args).

% 示例
Pid = spawn(fun() -> io:format("Hello from process~n") end).

发送消息

1
2
3
4
Pid ! Message.

% 示例
Pid ! {hello, self()}.

接收消息

1
2
3
4
5
receive
Pattern1 -> Expression1;
Pattern2 -> Expression2;
after Timeout -> TimeoutExpression
end.

模式匹配

1
{ok, Value} = {ok, 42}.

常用内置函数 (BIFs)

列表操作

1
2
3
4
lists:append(List1, List2).
lists:map(Function, List).
lists:filter(Function, List).
lists:foldl(Function, Acc, List).

元组操作

1
2
3
element(N, Tuple).
setelement(N, Tuple, Value).
tuple_size(Tuple).

字符串操作

1
2
3
string:len(String).
string:concat(String1, String2).
string:tokens(String, Delimiters).

文件操作

1
2
3
file:read_file(Filename).
file:write_file(Filename, Data).
file:delete(Filename).

列表操作

1
2
lists:map(fun(X) -> X * 2 end, [1, 2, 3]).
lists:filter(fun(X) -> X rem 2 == 0 end, [1, 2, 3, 4]).

字符串操作

1
2
string:len("Hello").
string:upper("hello").

文件操作

1
2
3
{ok, File} = file:open("test.txt", [write]).
file:write(File, "Hello, file!").
file:close(File).

示例:简单的服务器

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
-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.

并发编程

创建进程

1
Pid = spawn(Module, Function, Args).

发送消息

1
Pid ! Message.

接收消息

1
2
3
4
5
receive
Pattern1 -> Actions1;
Pattern2 -> Actions2;
...
end.

链接进程

1
2
link(Pid).
unlink(Pid).

监控进程

1
2
MonitorRef = erlang:monitor(process, Pid).
erlang:demonitor(MonitorRef).

错误处理

捕获异常

1
2
3
4
5
try Expression of
Pattern -> Result
catch
Class:Reason -> Handler
end.

常见异常类型

  • throw
  • error
  • exit

错误处理

1
2
3
4
5
try Expression of
Pattern -> Result
catch
Type:Reason -> ErrorHandlingExpression
end.

分布式编程

启动分布式节点

1
erl -name nodename@hostname -setcookie Cookie

连接节点

1
net_adm:ping(Node).

发送消息到远程节点

1
{remote_process, 'remote_node@host'} ! Message.

OTP 框架

定义 GenServer

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
-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}.

使用 GenServer

1
2
3
gen_server:start_link({local, Name}, Module, Args, Options).
gen_server:call(ServerRef, Request).
gen_server:cast(ServerRef, Msg).

测试

编写 EUnit 测试

1
2
3
4
5
6
7
8
9
10
11
-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)}
].

运行 EUnit 测试

1
2
# 在命令行中运行
erl -eval "eunit:test(module_name)" -s init stop

另见


评论
avatar
竹山一叶
技术分享 个人心得
Follow Me
公告
欢迎光临小站,这里是我日常工作和学习中收集和整理的总结,希望能对你有所帮助:)

本站的内容经过个人加工总结而来,也参考了网友们分享的资料,如有侵权,请第一时间联系我,我将及时进行修改或删除😊
最新文章
网站资讯
文章数目 :
436
已运行时间 :
本站总字数 :
431.5k
本站访客数 :
本站总访问量 :
最后更新时间 :
文章归档文章分类文章标签复制本文标题复制本文地址
随便逛逛