O'Reilly logo

Go Standard Library Cookbook by Radomir Sohlich

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

How to do it…

  1. Open the console and create the folder chapter01/recipe11.
  2. Navigate to the directory.
  3. Create the main.go file with the following content:
        package main        import (          "fmt"          "io"          "log"          "os"          "os/signal"          "syscall"          "time"        )        var writer *os.File        func main() {          // The file is opened as          // a log file to write into.          // This way we represent the resources          // allocation.          var err error          writer, err = os.OpenFile(fmt.Sprintf("test_%d.log",                time.Now().Unix()), os.O_RDWR|os.O_CREATE, os.ModePerm)          if err != nil {            panic(err)          }          // The code is running in a goroutine          // independently. So in case the program is          // terminated from outside, we need to          // let the goroutine know via the closeChan          closeChan := make(chan bool)          go func() {            for { time.Sleep(time.Second) ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required