12. Accessing Files and the Environment

If you’re going to write any nontrivial Go code, eventually you will get to the point where you need to interact with things outside of your program. The most common mechanism for persistent storage on modern systems is the filesystem, and it’s quite rare to see a program that doesn’t need to read files, even if it isn’t writing any.

If you want to write portable code, the filesystem is one of the more tricky areas that you have to deal with. Any modern operating system provides a file store, which is a mapping from filenames to sequences of bytes. Beyond that, the semantics can vary considerably.

Manipulating Paths

 1  package main 2  import "fmt" 3  import "path" 4  import ...

Get The Go Programming Language Phrasebook now with the O’Reilly learning platform.

O’Reilly members experience live online training, plus books, videos, and digital content from nearly 200 publishers.