fsx

package
v3.12.0-alpha.1 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Nov 5, 2021 License: GPL-3.0 Imports: 3 Imported by: 0

Documentation

Overview

Package fsx contains io/fs extensions.

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func OpenFile

func OpenFile(pathname string) (fs.File, error)

OpenFile is a wrapper for os.OpenFile that ensures that we're opening a file rather than a directory. If you are opening a directory, this func returns an *os.PathError error with Err set to syscall.EISDIR.

As mentioned in CONTRIBUTING.md, this is the function you SHOULD be using when opening files.

Example (OpeningDir)
package main

import (
	"errors"
	"log"
	"syscall"

	"github.com/ooni/probe-cli/v3/internal/fsx"
)

func main() {
	filep, err := fsx.OpenFile("testdata")
	if !errors.Is(err, syscall.ENOENT) {
		log.Fatal("unexpected error", err)
	}
	if filep != nil {
		log.Fatal("expected nil fp")
	}
}
Output:

Example (OpeningFile)
package main

import (
	"context"
	"fmt"
	"log"
	"path/filepath"

	"github.com/ooni/probe-cli/v3/internal/fsx"
	"github.com/ooni/probe-cli/v3/internal/netxlite"
)

func main() {
	filep, err := fsx.OpenFile(filepath.Join("testdata", "testfile.txt"))
	if err != nil {
		log.Fatal("unexpected error", err)
	}
	data, err := netxlite.ReadAllContext(context.Background(), filep)
	if err != nil {
		log.Fatal("unexpected error", err)
	}
	fmt.Printf("%d\n", len(data))
}
Output:

Types

This section is empty.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL