package main

import (
	"fmt"
	"io"
	"os"
)

func main() {
	if err := write("readme.txt", "This is a readme file"); err != nil {
		panic(err)
	}
}

func write(fileName string, text string) error {
	file, err := os.Create(fileName)
	if err != nil {
		return err
	}
	defer func() {
		fmt.Printf("closing %s\n", file.Name())
		file.Close()
	}()
	_, err = io.WriteString(file, text)
	if err != nil {
		return err
	}
	// explicitly close the file
	if err := file.Close(); err != nil {
		return err
	}
	return nil
}
