What's the correct way to read a file using Google AppEngine (Go)?
In Java I read there are context.getResourceAsStream
, is there any equivalent function for that?
What's the correct way to read a file using Google AppEngine (Go)?
In Java I read there are context.getResourceAsStream
, is there any equivalent function for that?
You can read from files on App Engine the same way you can read from files in a Go app running on your computer.
Some things to keep in mind:
You should use relative file paths instead of absolute. The working directory is the root folder of your app (where the
app.yaml
file resides).Only files that are application files can be read by Go code, so if you want to read a file from Go code, the file must not be matched by a static file pattern (or if it must be available as a static file too,
application_readable
option must be specified at the static file handler which includes/applies to the file, details).The latter is detailed on the Application configuration page, Section Static file handlers. Quoting the relevant part:
So let's say you have a folder
data
in your app's root (next toapp.yaml
), and a filelist.txt
in it. You may read its content like this:Or if you want / need an io.Reader (
os.File
implementsio.Reader
along with many other):Related questions:
Google App Engine Golang no such file or directory
Static pages return 404 in Google App Engine
How do I store the private key of my server in google app engine?