Static files in Go
bouk.co
bouk.co
Interestingly the X Window System took the opposite approach for its images. Rather than making C accommodate their images, they made their images accommodate C! https://en.wikipedia.org/wiki/X_BitMap and https://en.wikipedia.org/wiki/X_PixMap are both image formats that consist of C code.
Seen that way, resources are over-engineered. The basic compiler feature we weant is something like:
const char blob[] = __include_binary__("blob.dat");
But if I could do that, then probably the next thing I would do is invent some macro-magic to provide a resource-like namespace on top of the feature.- display an icon for an executable
- automatic localisation of application menus etc.
So FindResource() takes three parameters: a type (icon, string, dialog, bitmap, menu etc), a name, and a language ID. That means that changing the language ID can transparently select a different UI layout and text as required.
Meanwhile Explorer (and its ancestor Program Manager) will display the first icon in the resource file as the default program icon.
Resources are just a separate section within the PE executable format.
So it sounds like the critical thing that Windows resources have over the thing I outlined is that they and their namespaces are relatively transparent to external tools such as the Program Manager.
The critical thing is that MS could impose a standard. Anyone can write tools to put such a section to their ELF binaries. But it would be useless because then no one would know to read it.
This was already like that on the 16 bit days.
For example, with Turbo Pascal for Windows, you only needed to do, after generating the resources via the resource compiler
{$R resource name}
and then make use of FindResource/LoadResource/LoadFromResourceName[0]: (shameless plug) http://stackoverflow.com/questions/8923097/compile-a-binary-...
func AtRuntime(writer io.Writer){
templ := template.Must(template.New("blah").Parse(templateText))
templ.Execute(writer, someStructhere)
}
Though most sane devs would put the template parsing code in init()Couldn't you just put them in the global namespace in the package? I also don't see the need for init since this is just parsing / compiling a template one time. Maybe I'm just not a sane dev? I just put all my template and regex "must" statements as global vars in my package close to where they're used.
A thing can only be fast relative to another thing. If ego were the only templating solution available to software developers it would be meaningless to describe it as "fast". I have never used ego and do not know how much faster it is than the alternatives (if at all).
However, ego templates are compiled to go source which ends up as machine code in your program that just writes out the strings. I believe most alternative templating libraries parse your template into a data structure at run time and then need to walk that data structure every time you want to render the template in a new context.
So, consider a template like "<div> <h1> {{title}} </h1> <p> {{description}} </p> </div>"
Using the ego strategy the code that has to execute to render it amounts more or less to a linear series of 5 calls to a method that appends strings into a buffer.
Using an interpreted-template strategy, i don't know exactly what that machine code is going to look like but it necessarily involves a lot of consulting the parsed template to see what the next step is, and maybe even a lot of lookups of strings in hash tables.
Aside: In the Java world, the fastest template rendering engines probably compile a template into JVM bytecode which can then be loaded and executed in the current process, yielding what could reasonably be considered the best of both worlds.