Embed Directive in Wolfram Language

Here’s the translation of the Go code to Wolfram Language, presented in Markdown format suitable for Hugo:

Our first example demonstrates how to embed files and folders into a Wolfram Language program. While Wolfram Language doesn’t have a direct equivalent to Go’s embed directive, we can achieve similar functionality using Import and FileNameJoin.

(* Import necessary functions *)
Needs["GeneralUtilities`"]

(* Define the path to our folder *)
folderPath = FileNameJoin[{Directory[], "folder"}]

(* Read contents of a single file *)
fileString = Import[FileNameJoin[{folderPath, "single_file.txt"}], "String"]

(* Read contents of a file as bytes *)
fileByte = ImportByteArray[FileNameJoin[{folderPath, "single_file.txt"}]]

(* Read multiple files *)
fileList = FileNames["*.hash", folderPath]
folder = Association[# -> Import[#, "String"] & /@ fileList]

(* Main function *)
main[] := Module[{},
  (* Print out the contents of single_file.txt *)
  Print[fileString];
  Print[FromCharacterCode[fileByte]];
  
  (* Retrieve some files from the folder *)
  Print[folder[FileNameJoin[{folderPath, "file1.hash"}]]];
  Print[folder[FileNameJoin[{folderPath, "file2.hash"}]]];
]

(* Run the main function *)
main[]

To run this example, you need to create a folder structure similar to the Go example:

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

Then, you can run the Wolfram Language script:

$ wolframscript -file embed-files.wl
hello wolfram
hello wolfram
123
456

In this Wolfram Language version:

  1. We use Import to read the contents of files, which is similar to the embed directive in Go.
  2. FileNameJoin is used to create platform-independent file paths.
  3. Instead of embed.FS, we use an Association to create a simple key-value store of file names and their contents.
  4. The main function is defined as a module to encapsulate the main logic.

While Wolfram Language doesn’t have a built-in way to embed files at compile-time like Go does, this approach allows you to read and use file contents at runtime in a similar manner.