【问题标题】:Erlang - how to concatenate bit stringsErlang - 如何连接位串
【发布时间】:2016-07-24 06:00:37
【问题描述】:

我正在尝试连接位串

 cowboy_req:reply(

               200, #{<<"content-type">> => <<"text/html">>},

               <<"<div style='color:#FF0'>">> ++ cowboy_req:host(Req) ++ <<"</div>">> , 

               Req
    )

但由于++ 运算符,它会给出运行时错误。如何连接两个位串?

【问题讨论】:

标签: erlang bitstring


【解决方案1】:

你在这里拥有的是普通的二进制文件,而不是具体的位串。

如果您真的想连接它们,请将 cowboy_req:host(Req) 存储在一个变量中,然后连接 3 个二进制文件:

Host = cowboy_req:host(Req),
cowboy_req:reply(
    200,
    #{<<"content-type">> => <<"text/html">>},
    <<"<div style='color:#FF0'>", Host/binary, "</div>">>, 
    Req
)

请注意,由于cowboy_req:reply 接受iodata(),因此返回这样的列表通常更有效:

cowboy_req:reply(
    200,
    #{<<"content-type">> => <<"text/html">>},
    [<<"<div style='color:#FF0'>">>, cowboy_req:host(Req), <<"</div>">>], 
    Req
)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-01-15
    • 1970-01-01
    • 1970-01-01
    • 2012-03-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多