Skip to content

language: Add ability to import string constant or data from an external file. #7024

@gopherbot

Description

@gopherbot

by tdhutt:

Hi,

A really useful feature which I always missed in C++, and would be great in Go is the
ability to have a string or []byte constant that is initialised at compile time with the
contents of an external file.

This would be really useful in a number of instances:

* Embedding graphics shader programs
* Embedding icons and other graphics needed for programs.
* Well, embedding anything really. Go currently has no decent way to do this.

Here is a syntax proposal:

const string = #`filename.txt`
const []byte = #`filename.dat`

(or any other combination of unambiguous characters:  #"foo",
@"foo", @`foo`, $"foo", !"foo", etc.)

Or you could overload "import", since it is already a keyword and matches the
meaning well. Might be confusing for the reader though, and maybe make the parser more
complicated.

const string = import("filename.txt")
const []byte = import("filename.dat")

Or just make a new keyword and hope it doesn't break anyone's code.

const string = contents("filename.txt")

If the file is assigned to a []byte it has the obvious meaning. If it is assigned to a
string it is read as UTF-8 only.

I can't see any obvious downsides to this really. If I were to write a decent patch for
this would it be accepted?

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions