Skip to content

proposal: More Type-Safe File Handling #142

Closed
@gopherbot

Description

@gopherbot

by vomjom:

I propose that Go implements a Reader interface for files that are read-
only and a Writer interface for files that are write-only.

The motivation is this type of program:

package main

import "os"

func main() {
    f, _ := os.Open("test.out", os.O_CREATE | os.O_RDONLY, 0644);
    defer f.Close();

    f.WriteString("foobar");
}


It succeeds in running, but doesn't output anything.  This kind of error 
can be caught safely at compile-time if WriteString() only belonged to a 
Writer interface.

Specifically, here's my proposal:

1. Get rid of os.O_RDONLY and os.O_WRONLY.

2. Keep os.Open.  os.Open would mean that you are opening a file for both 
reading and writing.

3. Make an os.OpenRead() and os.OpenWrite().  These would return read only 
and write only interfaces to files.

These Reader and Writer interfaces should be generic enough that they can 
be used in other settings (sockets and pipes, for example).

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