UP | HOME

输出至终端

下面就用一个简单的示例程序来说明如何使用 io:format 函数:

1> io:format("hello world~n", []).
hello world
ok

2> io:format("this outputs one Erlang term: ~w~n", [hello]).
this outputs one Erlang term: hello
ok

3> io:format("this outputs two Erlang terms: ~w~w~n", [hello, world]).
this outputs two Erlang terms: helloworld
ok

4> io:format("this outputs two Erlang terms: ~w ~w~n", [hello, world]).
this outputs two Erlang terms: hello world
ok


5> io:format("hello world ~w ~w", [1]). 
** exception error: bad argument
     in function  io:format/3
        called as io:format(<0.64.0>,"hello world ~w ~w",[1])

format/2 (2 表示两个参数)接受两个列表作为参数:

这并不是 Erlang 系统中的错误,而是经过深思熟虑后的一种策略,稍后会看到,Erlang 有着非常完善的错误处理机制来处理这些错误

请注意,io:format 函数崩溃并不是说 Erlang shell 本身崩溃了

Next:完整实例

Previous:使用手册

Home:顺序编程