Skip to content

Commit 33e63eb

Browse files
odeke-emadg
authored andcommitted
os: add more examples
Updates #16360. Adds examples for: + Chmod + Chtimes + FileMode Change-Id: I1b61ee0392fa3774593a7f36aaf0fa1e484c778b Reviewed-on: https://go-review.googlesource.com/28963 Run-TryBot: Andrew Gerrand <[email protected]> TryBot-Result: Gobot Gobot <[email protected]> Reviewed-by: Andrew Gerrand <[email protected]>
1 parent a562351 commit 33e63eb

File tree

1 file changed

+34
-0
lines changed

1 file changed

+34
-0
lines changed

src/os/example_test.go

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,10 @@
55
package os_test
66

77
import (
8+
"fmt"
89
"log"
910
"os"
11+
"time"
1012
)
1113

1214
func ExampleOpenFile() {
@@ -18,3 +20,35 @@ func ExampleOpenFile() {
1820
log.Fatal(err)
1921
}
2022
}
23+
24+
func ExampleChmod() {
25+
if err := os.Chmod("some-filename", 0644); err != nil {
26+
log.Fatal(err)
27+
}
28+
}
29+
30+
func ExampleChtimes() {
31+
mtime := time.Date(2006, time.February, 1, 3, 4, 5, 0, time.UTC)
32+
atime := time.Date(2007, time.March, 2, 4, 5, 6, 0, time.UTC)
33+
if err := os.Chtimes("some-filename", atime, mtime); err != nil {
34+
log.Fatal(err)
35+
}
36+
}
37+
38+
func ExampleFileMode() {
39+
fi, err := os.Stat("some-filename")
40+
if err != nil {
41+
log.Fatal(err)
42+
}
43+
44+
switch mode := fi.Mode(); {
45+
case mode.IsRegular():
46+
fmt.Println("regular file")
47+
case mode.IsDir():
48+
fmt.Println("directory")
49+
case mode&os.ModeSymlink != 0:
50+
fmt.Println("symbolic link")
51+
case mode&os.ModeNamedPipe != 0:
52+
fmt.Println("named pipe")
53+
}
54+
}

0 commit comments

Comments
 (0)