Embed Directive in Lisp

Here’s the translation of the Go code to Lisp, formatted in Markdown suitable for Hugo:

Our first program demonstrates embedding files and folders into the binary at build time. Here’s the full source code:

(defpackage :embed-example
  (:use :cl))

(in-package :embed-example)

;; In Common Lisp, there's no direct equivalent to Go's embed directive.
;; Instead, we'll simulate file embedding using a hash table.

(defparameter *embedded-files* (make-hash-table :test #'equal))

;; Simulating file embedding
(setf (gethash "folder/single_file.txt" *embedded-files*) "hello lisp")
(setf (gethash "folder/file1.hash" *embedded-files*) "123")
(setf (gethash "folder/file2.hash" *embedded-files*) "456")

(defun read-embedded-file (filename)
  (gethash filename *embedded-files*))

(defun main ()
  ;; Print out the contents of 'single_file.txt'
  (format t "~A~%" (read-embedded-file "folder/single_file.txt"))
  (format t "~A~%" (read-embedded-file "folder/single_file.txt"))

  ;; Retrieve some files from the embedded folder
  (format t "~A~%" (read-embedded-file "folder/file1.hash"))
  (format t "~A~%" (read-embedded-file "folder/file2.hash")))

In Common Lisp, there’s no direct equivalent to the embed directive. Instead, we simulate file embedding using a hash table. The *embedded-files* hash table acts as our virtual file system, storing the content of our “embedded” files.

We define a read-embedded-file function that retrieves the content of a file from our simulated embedded file system.

To run the example:

$ sbcl --script embed-example.lisp
hello lisp
hello lisp
123
456

This example demonstrates how we can simulate embedding files in Common Lisp. While it doesn’t actually embed files into the binary at compile time like in the original example, it provides a similar functionality at runtime.

In a real-world scenario, you might want to consider using libraries like ASDF (Another System Definition Facility) for managing project files and resources in Common Lisp.