blob: 5749194871b795f6e49cb8b64d799abbc1e2e1b2 [file] [log] [blame]
Jaana Burcu Dogan428d79b2016-08-28 23:11:00 -07001// 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
5package os_test
6
7import (
Emmanuel Odeke33e63eb2016-09-11 05:06:29 -07008 "fmt"
Jaana Burcu Dogan428d79b2016-08-28 23:11:00 -07009 "log"
10 "os"
Emmanuel Odeke33e63eb2016-09-11 05:06:29 -070011 "time"
Jaana Burcu Dogan428d79b2016-08-28 23:11:00 -070012)
13
14func 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}
Emmanuel Odeke33e63eb2016-09-11 05:06:29 -070023
Kevin Burke6d32b1a2017-02-28 16:28:34 -080024func 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
Emmanuel Odeke33e63eb2016-09-11 05:06:29 -070038func ExampleChmod() {
39 if err := os.Chmod("some-filename", 0644); err != nil {
40 log.Fatal(err)
41 }
42}
43
44func 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
52func ExampleFileMode() {
Taro Aoki4f299f92017-07-01 19:13:03 +090053 fi, err := os.Lstat("some-filename")
Emmanuel Odeke33e63eb2016-09-11 05:06:29 -070054 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}
Sina Siadat22872962016-08-08 02:04:52 +043069
70func 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}
Jean-Nicolas Moal6d702d82016-08-22 19:02:33 +020078
79func init() {
80 os.Setenv("USER", "gopher")
81 os.Setenv("HOME", "/usr/gopher")
82 os.Unsetenv("GOPATH")
83}
84
85func ExampleExpandEnv() {
86 fmt.Println(os.ExpandEnv("$USER lives in ${HOME}."))
87
88 // Output:
89 // gopher lives in /usr/gopher.
90}
91
92func ExampleLookupEnv() {
93 show := func(key string) {
94 val, ok := os.LookupEnv(key)
95 if !ok {
96 fmt.Printf("%s not set\n", key)
97 } else {
98 fmt.Printf("%s=%s\n", key, val)
99 }
100 }
101
102 show("USER")
103 show("GOPATH")
104
105 // Output:
106 // USER=gopher
107 // GOPATH not set
108}
109
110func ExampleGetenv() {
111 fmt.Printf("%s lives in %s.\n", os.Getenv("USER"), os.Getenv("HOME"))
112
113 // Output:
114 // gopher lives in /usr/gopher.
115}
116
117func ExampleUnsetenv() {
118 os.Setenv("TMPDIR", "/my/tmp")
119 defer os.Unsetenv("TMPDIR")
120}