Categorygithub.com/mithrandie/go-file/v2
modulepackage
2.1.0
Repository: https://github.com/mithrandie/go-file.git
Documentation: pkg.go.dev

# README

go-file

Package file is a Go library to open files with file locking depending on the system.

Build Status GoDoc License: MIT

Install

go get github.com/mithrandie/go-file

Requirements

Go 1.17 or later (cf. Getting Started - The Go Programming Language)

Supported Systems

Currently, file locking on the following systems are supported.

darwin dragonfly freebsd linux netbsd openbsd solaris

Advisory Lock

windows

Mandatory Lock

android nacl plan9 zos

Not Supported

Example

package main

import (
	"bufio"
	"context"
	"fmt"
	"time"
	 
	"github.com/mithrandie/go-file/v2"
)

func main() {
	// Try to lock and open the file with shared lock
	fp, err := file.TryOpenToRead("/path/to/file")
	if err != nil {
		panic(err)
	}
	defer func() {
		if e := file.Close(fp); e != nil {
			println(e.Error())
		}
	}()

	s := bufio.NewScanner(fp)
	for s.Scan() {
		fmt.Println(s.Text())
	}

	// Open the file with shared lock.
	// If the file is already locked, tries to lock repeatedly until the conditions is met.
	ctx, cancel := context.WithTimeout(context.Background(), 3*time.Second)
	cfp, err := file.OpenToReadContext(ctx, 50*time.Millisecond, "/path/to/file2")
	if err != nil {
		panic(err)
	}
	defer func() {
		cancel()
		if e := file.Close(cfp); e != nil {
			println(e.Error())
		}
	}()

	cs := bufio.NewScanner(cfp)
	for cs.Scan() {
		fmt.Println(cs.Text())
	}
}

# Functions

Unlocks and closes the file.
Opens the file with exclusive locking.
Places the exclusive lock on the file.
Places the exclusive lock on the file.
LockEX places an exclusive lock on the file.
LockSH places a shared lock on the file.
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
Opens the file with passed locking function.
Opens the file with passed locking function.
Opens the file with shared lock.
Opens the file with shared lock.
Opens the file with exclusive lock.
Opens the file with exclusive lock.
Places the shared lock on the file.
Places the shared lock on the file.
Places the exclusive lock on the file.
TryLockEX places an exclusive lock on the file.
TryLockSH places a shared lock on the file.
Tries to lock and opens the file with shared lock.
Tries to lock and opens the file with exclusive lock.
Places the shared lock on the file.
Unlock the file.

# Structs

No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author