.\" Made by website/plain/build.mjs from website/words/embed.md; change that file. .TH SG-EMBED 7 2026-11-28 "SG" "SG reference" .SH NAME embed \- embed("logo.gif"): the bytes of a file beside the source, read when the program is compiled and kept in it .SH SYNOPSIS .TP \fBconst byte[] Logo = embed("logo.gif")\fR the bytes of a file, as a byte[N] counted from the file .TP \fBconst byte[14] Logo = embed("logo.gif")\fR the same, with the count written: it must be the size of the file .TP \fBembed("images/logo.gif")\fR a file in a folder below the source file .TP \fBpublic const byte[] Logo = embed("logo.gif")\fR bytes other modules can read .TP \fBLogo.length(), Logo[i]\fR in another const or a const assert, found when the program is compiled .SH DESCRIPTION \fBembed("logo.gif")\fR reads a file when the program is compiled and gives its bytes as a \fBbyte[N]\fR, where \fBN\fR is the size of the file. It is written only where a const is given its value, so the bytes are a const array like any other: \fBbyte[]\fR after \fBconst\fR takes the count from the file, other consts and a \fBconst assert\fR can read its length and its items, \fBread Logo\fR lends it where it lies, and \fBvar copy = Logo\fR makes a copy of it. A \fBstring\fR is not given this way; \fBstring.fromUtf8(Logo)\fR makes a text of the bytes when the program runs, and checks that they are UTF-8. .PP The path is a text written out in the call: not a const, and not a text with values in it, since it is needed when the program is compiled. It is written from the folder of the source file that names it (in a module, that file's folder, not the folder of the program), with \fB/\fR between the folders. It has no \fB\&..\fR, no \fB\&.\fR, no empty part, no \fB\e\fR or \fB:\fR, and does not start with \fB/\fR\&. The file lies in that folder or below it, also where a link leads, so a module of someone else's takes in only the files it carries. .PP The file holds 1 byte to 64 MB. A file that is missing, empty, larger, or that cannot be read is an error when the program is compiled. .PP A built program keeps the bytes in the data it only reads, and needs the file no more; two consts of the same bytes are kept once. The interpreter reads the file each time it checks the program, before it runs, and holds the bytes in about eight times their size. .SH EXAMPLES .SS "A text the program carries" The file tool/usage.txt: .PP .RS 4 .nf Usage: tool [options] file -n number the lines -q say nothing .fi .RE .PP The file tool/main.sg: .PP .RS 4 .nf const byte[] Usage = embed("usage.txt") void main() { var text = string.fromUtf8(Usage) ?? "not UTF-8" print(text.trimEnd()) print($"{Usage.length()} bytes") } .fi .RE .PP .RS 4 .nf $ cd tool $ sg build main.sg -o tool $ rm usage.txt $ ./tool Usage: tool [options] file -n number the lines -q say nothing 68 bytes .fi .RE .PP The file is read when \fBsg build\fR compiles the program, so the program still prints it after the file is gone. \fBstring.fromUtf8\fR turns the bytes into a text when the program runs; \fBconst string Usage = embed(...)\fR is an error, \fBexpected `string`, found `byte[68]`\fR\&. .SS "Checked when the program is compiled" The file gif/main.sg: .PP .RS 4 .nf const byte[] Image = embed("dot.gif") const nint Size = Image.length() const assert(Size >= 13, "a GIF starts with 13 bytes") const assert(Image[0] == 0x47 && Image[1] == 0x49 && Image[2] == 0x46, "not a GIF") const ushort Width = (ushort)Image[6] | (ushort)Image[7] << 8 void main() { print($"{Size} bytes, {Width} pixels wide") } .fi .RE .PP .RS 4 .nf $ cd gif $ printf 'GIF89a\e002\e001\e001\e000\e200\e000\e000;' > dot.gif $ sg main.sg 14 bytes, 258 pixels wide $ printf 'Not a picture.' > dot.gif $ sg main.sg error: const assert failed: not a GIF --> main.sg:4:14 4 | const assert(Image[0] == 0x47 && Image[1] == 0x49 && Image[2] == 0x46, "not a GIF") | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ .fi .RE .PP \fBprintf\fR writes a small GIF, byte by byte. Its length and its items are known when the program is compiled, so other consts are made of them, and a file that is not a GIF stops the compiler, not the program. .SS "Read into a struct" The file screen/main.sg: .PP .RS 4 .nf packed struct Screen : Formattable { byte[6] signature ushort width ushort height byte flags byte background byte aspect } const byte[] Image = embed("dot.gif") const assert(Image.length() >= 13, "a GIF starts with 13 bytes") void main() { var screen = Screen.fromBytes(Image[0..13]) print(screen) print(string.fromUtf8(screen.signature[0..6]) ?? "not UTF-8") print($"{screen.width} x {screen.height}") } .fi .RE .PP .RS 4 .nf $ cd screen $ printf 'GIF89a\e002\e001\e001\e000\e200\e000\e000;' > dot.gif $ sg main.sg Screen { signature = [71, 73, 70, 56, 57, 97], width = 258, height = 1, flags = 128, background = 0, aspect = 0 } GIF89a 258 x 1 .fi .RE .PP A GIF starts with thirteen bytes whose numbers of 16 bits lie at bytes 6 and 8, little-endian, so a [\fBpacked\fR](packed.md) struct lays them out as the file does. \fBfromBytes\fR takes exactly as many bytes as the struct has; the \fBconst assert\fR makes sure the file has them. .SS "From a module" The file shop/assets/files/banner.txt: .PP .RS 4 .nf Welcome to the shop. .fi .RE .PP The file shop/assets/texts.sg: .PP .RS 4 .nf public const byte[] Banner = embed("files/banner.txt") .fi .RE .PP The file shop/main.sg: .PP .RS 4 .nf use assets void main() { print(string.fromUtf8(Banner) ?? "not UTF-8") print(assets.Banner.length()) } .fi .RE .PP .RS 4 .nf $ cd shop $ sg main.sg Welcome to the shop. 21 .fi .RE .PP The path is written from the folder of \fBtexts.sg\fR, the file that names it: \fBfiles/banner.txt\fR is below \fBassets/\fR\&. The module carries its own file, and a program that uses it reads the bytes as any \fBpublic const\fR\&. The empty line is the end of the line of the file, which \fBprint\fR writes before its own. .SH "WHAT THE COMPILER REFUSES" .SS "Outside a const" .RS 4 .nf void main() { var bytes = embed("logo.gif") print(bytes.length()) } .fi .RE .PP .RS 4 .nf error: `embed` is written where a const is given its value --> example.sg:2:17 2 | var bytes = embed("logo.gif") | ^^^^^^^^^^^^^^^^^ hint: `const byte[] Logo = embed("logo.gif")`, then `Logo` where the bytes are needed .fi .RE .PP A \fBstatic\fR is refused the same way. .SS "A path that is not written out" .RS 4 .nf const string Name = "logo.gif" const byte[] Logo = embed(Name) void main() { print(Logo.length()) } .fi .RE .PP .RS 4 .nf error: `embed` takes the path of a file, written out as a text the file is read when the program is compiled, so its path is known then --> example.sg:2:21 2 | const byte[] Logo = embed(Name) | ^^^^^^^^^^^ hint: `const byte[] Logo = embed("logo.gif")` .fi .RE .SS "A file above the folder" .RS 4 .nf const byte[] Secret = embed("../keys/secret.txt") void main() { print(Secret.length()) } .fi .RE .PP .RS 4 .nf error: `../keys/secret.txt` is not a file beside this one a file of `embed` lies in the folder of the file it is written in or below it, and its path is written from there with `/`, without `..` --> example.sg:1:23 1 | const byte[] Secret = embed("../keys/secret.txt") | ^^^^^^^^^^^^^^^^^^^^^^^^^^^ hint: `embed("logo.gif")`, `embed("images/logo.gif")` .fi .RE .PP A path from the root (\fBembed("/etc/hosts")\fR) and one with a drive or \fB\e\fR are refused the same way; \fB\&./logo.gif\fR is refused with the path to write, \fBlogo.gif\fR\&. A link beside the file that leads outside its folder is refused with where it leads. .SS "A file that is not there" .RS 4 .nf const byte[] Logo = embed("logo.gif") void main() { print(Logo.length()) } .fi .RE .PP .RS 4 .nf error: there is no file `logo.gif` beside this file it is looked for at logo.gif --> example.sg:1:21 1 | const byte[] Logo = embed("logo.gif") | ^^^^^^^^^^^^^^^^^ .fi .RE .SS "An empty file, a file too large, and a count that differs" The file sizes/empty.sg: .PP .RS 4 .nf const byte[] Nothing = embed("empty.bin") void main() { print(Nothing.length()) } .fi .RE .PP The file sizes/large.sg: .PP .RS 4 .nf const byte[] Movie = embed("large.bin") void main() { print(Movie.length()) } .fi .RE .PP The file sizes/count.sg: .PP .RS 4 .nf const byte[4] Head = embed("five.bin") void main() { print(Head) } .fi .RE .PP .RS 4 .nf $ cd sizes $ : > empty.bin $ sg empty.sg error: the file is empty, and an array has at least one item --> empty.sg:1:24 1 | const byte[] Nothing = embed("empty.bin") | ^^^^^^^^^^^^^^^^^^ $ dd if=/dev/zero of=large.bin bs=1048576 count=65 2> /dev/null $ sg large.sg error: `large.bin` has 68157440 bytes, and `embed` takes a file of at most 64 MB the program carries the bytes, and so does the C it is compiled from --> large.sg:1:22 1 | const byte[] Movie = embed("large.bin") | ^^^^^^^^^^^^^^^^^^ $ printf 'abcde' > five.bin $ sg count.sg error: expected `byte[4]`, found `byte[5]` --> count.sg:1:22 1 | const byte[4] Head = embed("five.bin") | ^^^^^^^^^^^^^^^^^ .fi .RE .PP \fB: >\fR makes an empty file, and \fBdd\fR one of 65 MB. .SH "SEE ALSO" .BR sg-const (7), .BR sg-byte (7), .BR sg-assert (7), .BR sg-packed (7), .BR sg-string (7) .PP https://sg\-lang.org/reference/embed.html