Chinaunix首页 | 论坛 | 博客
  • 博客访问: 334090
  • 博文数量: 214
  • 博客积分: 4258
  • 博客等级: 上校
  • 技术积分: 2021
  • 用 户 组: 普通用户
  • 注册时间: 2010-12-02 09:16
个人简介

http://blog.csdn.net/ly21st http://ly21st.blog.chinaunix.net

文章分类

全部博文(214)

文章存档

2018年(16)

2015年(1)

2014年(2)

2012年(22)

2011年(173)

分类: C/C++

2018-01-29 18:00:54

os操作的简单例子

点击(此处)折叠或打开

  1. // os_mytest project main.go
  2. package main

  3. // Copyright 2016 The Go Authors. All rights reserved.
  4. // Use of this source code is governed by a BSD-style
  5. // license that can be found in the LICENSE file.ExampleChmod()

  6. import (
  7.     "fmt"
  8.     "log"
  9.     "os"
  10.     "time"
  11. )

  12. func ExampleOpenFile() {
  13.     f, err := os.OpenFile("notes.txt", os.O_RDWR|os.O_CREATE, 0755)
  14.     if err != nil {
  15.         log.Fatal(err)
  16.     }

  17.     if err := f.Close(); err != nil {
  18.         log.Fatal(err)
  19.     }
  20. }

  21. func ExampleOpenFile_append() {
  22.     // If the file doesn't exist, create it, or append to the file
  23.     f, err := os.OpenFile("access.log", os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0644)
  24.     if err != nil {
  25.         log.Fatal(err)
  26.     }
  27.     if _, err := f.Write([]byte("appended some data\n")); err != nil {
  28.         log.Fatal(err)
  29.     }
  30.     if err := f.Close(); err != nil {
  31.         log.Fatal(err)
  32.     }
  33. }

  34. func ExampleChmod() {
  35.     if err := os.Chmod("notes.txt", 0644); err != nil {
  36.         log.Fatal(err)
  37.     }
  38. }

  39. func ExampleChtimes() {
  40.     mtime := time.Date(2006, time.February, 1, 3, 4, 5, 0, time.UTC)
  41.     atime := time.Date(2007, time.March, 2, 4, 5, 6, 0, time.UTC)
  42.     if err := os.Chtimes("notes.txt", atime, mtime); err != nil {
  43.         log.Fatal(err)
  44.     }
  45. }

  46. func ExampleFileMode() {
  47.     fi, err := os.Lstat("notes.txt")
  48.     if err != nil {
  49.         log.Fatal(err)
  50.     }

  51.     switch mode := fi.Mode(); {
  52.     case mode.IsRegular():
  53.         fmt.Println("regular file")
  54.     case mode.IsDir():
  55.         fmt.Println("directory")
  56.     case mode&os.ModeSymlink != 0:
  57.         fmt.Println("symbolic link")
  58.     case mode&os.ModeNamedPipe != 0:
  59.         fmt.Println("named pipe")
  60.     }
  61. }

  62. func ExampleIsNotExist() {
  63.     filename := "a-nonexistent-file"
  64.     if _, err := os.Stat(filename); os.IsNotExist(err) {
  65.         fmt.Printf("file does not exist")
  66.     }
  67.     // Output:
  68.     // file does not exist
  69. }

  70. func init() {
  71.     os.Setenv("USER", "gopher")
  72.     os.Setenv("HOME", "/usr/gopher")
  73.     os.Unsetenv("GOPATH")
  74. }

  75. func ExampleExpandEnv() {
  76.     fmt.Println(os.ExpandEnv("$USER lives in ${HOME}."))

  77.     // Output:
  78.     // gopher lives in /usr/gopher.
  79. }

  80. func ExampleLookupEnv() {
  81.     show := func(key string) {
  82.         val, ok := os.LookupEnv(key)
  83.         if !ok {
  84.             fmt.Printf("%s not set\n", key)
  85.         } else {
  86.             fmt.Printf("%s=%s\n", key, val)
  87.         }
  88.     }

  89.     show("USER")
  90.     show("GOPATH")

  91.     // Output:
  92.     // USER=gopher
  93.     // GOPATH not set
  94. }

  95. func ExampleGetenv() {
  96.     fmt.Printf("%s lives in %s.\n", os.Getenv("USER"), os.Getenv("HOME"))

  97.     // Output:
  98.     // gopher lives in /usr/gopher.
  99. }

  100. func ExampleUnsetenv() {
  101.     os.Setenv("TMPDIR", "/my/tmp")
  102.     defer os.Unsetenv("TMPDIR")
  103. }

  104. func main() {
  105.     ExampleOpenFile()
  106.     ExampleOpenFile_append()
  107.     ExampleChmod()
  108.     ExampleChtimes()
  109.     ExampleFileMode()
  110.     ExampleIsNotExist()
  111.     fmt.Println()
  112.     ExampleExpandEnv()
  113.     ExampleLookupEnv()
  114.     ExampleGetenv()
  115.     ExampleUnsetenv()
  116. }
结果
[root@hadoop1 os]# ./os_mytest 
regular file
file does not exist
gopher lives in /usr/gopher.
USER=gopher
GOPATH not set
gopher lives in /usr/gopher.
[root@hadoop1 os]#
阅读(515) | 评论(0) | 转发(0) |
0

上一篇:服务器客户端例子

下一篇:文件路径分割

给主人留下些什么吧!~~