Sobes.tech
Junior — Senior

Possible issues in implementing the logger

livecode

Task condition

Let's consider a typical example of creating a simple logger that writes messages to a file. It's important to pay attention to potential errors in implementation, such as incorrect resource management, missing pointers in receiver methods, and others.

 type LogWriter interface {
    Log(message string) error
    Close() error
}

type DiskLogger struct{ outFile *os.File }

func NewDiskLogger(fileName string) (*DiskLogger, error) {
    f, err := os.Create(fileName)
    if err != nil {
        return nil, err
    }
    return &DiskLogger{f}, nil
}

func (dl DiskLogger) Log(message string) error {
    _, err := dl.outFile.WriteString(message + "\n")
    return err
}

func (dl DiskLogger) Close() error {
    return dl.outFile.Close()
}

In this code snippet, we can discuss what errors might occur during its use and how to fix them.