Embed Directive in Erlang

Here’s the translation of the Go code to Erlang, with explanations in Markdown format suitable for Hugo:

Erlang doesn’t have a direct equivalent to the embed directive, but we can achieve similar functionality using Erlang’s built-in file handling capabilities and the file module. Here’s how we can replicate the functionality:

-module(file_embed).
-export([main/0]).

main() ->
    % Read the contents of a single file
    {ok, FileString} = file:read_file("folder/single_file.txt"),
    io:format("~s", [FileString]),
    
    % Read the contents of multiple files
    {ok, Content1} = file:read_file("folder/file1.hash"),
    io:format("~s", [Content1]),
    
    {ok, Content2} = file:read_file("folder/file2.hash"),
    io:format("~s", [Content2]).

In Erlang, we don’t have a direct equivalent to the embed directive, which allows embedding files at compile time. Instead, we use the file module to read files at runtime.

The file:read_file/1 function is used to read the contents of a file. It returns a tuple {ok, Binary} where Binary is the contents of the file as a binary string.

We use io:format/2 to print the contents of the files. The ~s format specifier is used to print strings.

To run this example, first create the necessary files:

$ mkdir -p folder
$ echo "hello erlang" > folder/single_file.txt
$ echo "123" > folder/file1.hash
$ echo "456" > folder/file2.hash

Then compile and run the Erlang module:

$ erlc file_embed.erl
$ erl -noshell -s file_embed main -s init stop
hello erlang123456

Note that in Erlang, we don’t have the concept of embedding files at compile time like in the original example. Instead, we read the files at runtime. This approach is more flexible but might be slower for large files or when fast startup time is crucial.

If you need to package files with your Erlang application, you typically include them in your application’s directory structure and use relative paths to access them at runtime. For more complex scenarios, you might want to look into using the OTP (Open Telecom Platform) application structure, which provides conventions for organizing and packaging Erlang applications.