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 chapter06/recipe01.
  2. Navigate to the directory.
  3. Create the sample test.file with the content This is test file.
  4. Create the fileinfo.go file with the following content:
        package main        import (          "fmt"          "os"        )        func main() {          f, err := os.Open("test.file")          if err != nil {            panic(err)          }          fi, err := f.Stat()          if err != nil {            panic(err)          }          fmt.Printf("File name: %v\n", fi.Name())          fmt.Printf("Is Directory: %t\n", fi.IsDir())          fmt.Printf("Size: %d\n", fi.Size())          fmt.Printf("Mode: %v\n", fi.Mode())        }
  1. Execute the code by running go run fileinfo.go in the main Terminal.
  2. You will see the following output:

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