...
Run Format

Source file src/os/example_test.go

Documentation: os

     1  // Copyright 2016 The Go Authors. All rights reserved.
     2  // Use of this source code is governed by a BSD-style
     3  // license that can be found in the LICENSE file.
     4  
     5  package os_test
     6  
     7  import (
     8  	"fmt"
     9  	"log"
    10  	"os"
    11  	"time"
    12  )
    13  
    14  func ExampleOpenFile() {
    15  	f, err := os.OpenFile("notes.txt", os.O_RDWR|os.O_CREATE, 0755)
    16  	if err != nil {
    17  		log.Fatal(err)
    18  	}
    19  	if err := f.Close(); err != nil {
    20  		log.Fatal(err)
    21  	}
    22  }
    23  
    24  func ExampleOpenFile_append() {
    25  	// If the file doesn't exist, create it, or append to the file
    26  	f, err := os.OpenFile("access.log", os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0644)
    27  	if err != nil {
    28  		log.Fatal(err)
    29  	}
    30  	if _, err := f.Write([]byte("appended some data\n")); err != nil {
    31  		log.Fatal(err)
    32  	}
    33  	if err := f.Close(); err != nil {
    34  		log.Fatal(err)
    35  	}
    36  }
    37  
    38  func ExampleChmod() {
    39  	if err := os.Chmod("some-filename", 0644); err != nil {
    40  		log.Fatal(err)
    41  	}
    42  }
    43  
    44  func ExampleChtimes() {
    45  	mtime := time.Date(2006, time.February, 1, 3, 4, 5, 0, time.UTC)
    46  	atime := time.Date(2007, time.March, 2, 4, 5, 6, 0, time.UTC)
    47  	if err := os.Chtimes("some-filename", atime, mtime); err != nil {
    48  		log.Fatal(err)
    49  	}
    50  }
    51  
    52  func ExampleFileMode() {
    53  	fi, err := os.Lstat("some-filename")
    54  	if err != nil {
    55  		log.Fatal(err)
    56  	}
    57  
    58  	switch mode := fi.Mode(); {
    59  	case mode.IsRegular():
    60  		fmt.Println("regular file")
    61  	case mode.IsDir():
    62  		fmt.Println("directory")
    63  	case mode&os.ModeSymlink != 0:
    64  		fmt.Println("symbolic link")
    65  	case mode&os.ModeNamedPipe != 0:
    66  		fmt.Println("named pipe")
    67  	}
    68  }
    69  
    70  func ExampleIsNotExist() {
    71  	filename := "a-nonexistent-file"
    72  	if _, err := os.Stat(filename); os.IsNotExist(err) {
    73  		fmt.Printf("file does not exist")
    74  	}
    75  	// Output:
    76  	// file does not exist
    77  }
    78  
    79  func init() {
    80  	os.Setenv("USER", "gopher")
    81  	os.Setenv("HOME", "/usr/gopher")
    82  	os.Unsetenv("GOPATH")
    83  }
    84  
    85  func ExampleExpand() {
    86  	mapper := func(placeholderName string) string {
    87  		switch placeholderName {
    88  		case "DAY_PART":
    89  			return "morning"
    90  		case "USER":
    91  			return "Gopher"
    92  		}
    93  
    94  		return ""
    95  	}
    96  
    97  	fmt.Println(os.Expand("Good ${DAY_PART}, $USER!", mapper))
    98  
    99  	// Output:
   100  	// Good morning, Gopher!
   101  }
   102  
   103  func ExampleExpandEnv() {
   104  	fmt.Println(os.ExpandEnv("$USER lives in ${HOME}."))
   105  
   106  	// Output:
   107  	// gopher lives in /usr/gopher.
   108  }
   109  
   110  func ExampleLookupEnv() {
   111  	show := func(key string) {
   112  		val, ok := os.LookupEnv(key)
   113  		if !ok {
   114  			fmt.Printf("%s not set\n", key)
   115  		} else {
   116  			fmt.Printf("%s=%s\n", key, val)
   117  		}
   118  	}
   119  
   120  	show("USER")
   121  	show("GOPATH")
   122  
   123  	// Output:
   124  	// USER=gopher
   125  	// GOPATH not set
   126  }
   127  
   128  func ExampleGetenv() {
   129  	fmt.Printf("%s lives in %s.\n", os.Getenv("USER"), os.Getenv("HOME"))
   130  
   131  	// Output:
   132  	// gopher lives in /usr/gopher.
   133  }
   134  
   135  func ExampleUnsetenv() {
   136  	os.Setenv("TMPDIR", "/my/tmp")
   137  	defer os.Unsetenv("TMPDIR")
   138  }
   139  

View as plain text