Golang os 包与设置配置文件路径

一、os包的学习与使用(文件,目录,进程的操作)

参考\
golang语言中os包的学习与使用(文件,目录,进程的操作)

1.os中一些常用函数的使用
package main;

import (
    "os"
    "fmt"
    "time"
    "strings"
)

//os包中的一些常用函数

func main() {
    //获取主机名
    fmt.Println(os.Hostname());

    //获取当前目录
    fmt.Println(os.Getwd());

    //获取用户ID
    fmt.Println(os.Getuid());

    //获取有效用户ID
    fmt.Println(os.Geteuid());

    //获取组ID
    fmt.Println(os.Getgid());

    //获取有效组ID
    fmt.Println(os.Getegid());

    //获取进程ID
    fmt.Println(os.Getpid());

    //获取父进程ID
    fmt.Println(os.Getppid());

    //获取环境变量的值
    fmt.Println(os.Getenv("GOPATH"));

    //设置环境变量的值
    os.Setenv("TEST", "test");

    //改变当前工作目录
    os.Chdir("C:/");
    fmt.Println(os.Getwd());

    //创建文件
    f1, _ := os.Create("./1.txt");
    defer f1.Close();

    //修改文件权限
    if err := os.Chmod("./1.txt", 0777); err != nil {
        fmt.Println(err);
    }

    //修改文件所有者
    if err := os.Chown("./1.txt", 0, 0); err != nil {
        fmt.Println(err);
    }

    //修改文件的访问时间和修改时间
    os.Chtimes("./1.txt", time.Now().Add(time.Hour), time.Now().Add(time.Hour));

    //获取所有环境变量
    fmt.Println(strings.Join(os.Environ(), "\r\n"));

    //把字符串中带${var}或$var替换成指定指符串
    fmt.Println(os.Expand("${1} ${2} ${3}", func(k string) string {
        mapp := map[string]string{
            "1": "111",
            "2": "222",
            "3": "333",
        };
        return mapp[k];
    }));

    //创建目录
    os.Mkdir("abc", os.ModePerm);

    //创建多级目录
    os.MkdirAll("abc/d/e/f", os.ModePerm);

    //删除文件或目录
    os.Remove("abc/d/e/f");

    //删除指定目录下所有文件
    os.RemoveAll("abc");

    //重命名文件
    os.Rename("./2.txt", "./2_new.txt");

    //判断是否为同一文件
    //unix下通过底层结构的设备和索引节点是否相同来判断
    //其他系统可能是通过文件绝对路径来判断
    fs1, _ := f1.Stat();
    f2, _ := os.Open("./1.txt");
    fs2, _ := f2.Stat();
    fmt.Println(os.SameFile(fs1, fs2));

    //返回临时目录
    fmt.Println(os.TempDir());
}

比如golang 操作 系统环境变量小例子

func testEnv() {
    //临时设置 系统环境变量
    err := os.Setenv("XIAO", "xiaochuan")
    if err != nil {
        fmt.Println(err.Error())
    }
    //获取环境变量
    fmt.Println(os.Getenv("XIAO"))
    fmt.Println(os.Getenv("GOPATH"))
    //获取全部系统环境变量 获取的是 key=val 的[]string
    for _, v := range os.Environ() {
        str := strings.Split(v, "=")
        fmt.Printf("key=%s,val=%s \n", str[0], str[1])
    }
}
-------------------------
xiaochuan
D:\go\helloworld;D:\go
...
2.os中一些常用文件函数的使用
package main

import (
    "os"
    "fmt"
    "strconv"
)

//os包中关于文件的操作函数

func main() {
    //创建文件,返回一个文件指针
    f3, _ := os.Create("./3.txt");
    defer f3.Close();

    //以读写方式打开文件,如果不存在则创建文件,等同于上面os.Create
    f4, _ := os.OpenFile("./4.txt", os.O_RDWR|os.O_CREATE|os.O_TRUNC, 0666);
    defer f4.Close();

    //打开文件,返回文件指针
    f1, _ := os.Open("./1.txt");
    defer f1.Close();

    //修改文件权限,类似os.chmod
    f1.Chmod(0777);

    //修改文件所有者,类似os.chown
    f1.Chown(0, 0);

    //返回文件的句柄,通过NewFile创建文件需要文件句柄
    fmt.Println(f1.Fd());

    //从文件中读取数据
    buf := make([]byte, 128);
    //read每次读取数据到buf中
    for n, _ := f1.Read(buf); n != 0; n, _ = f1.Read(buf) {
        fmt.Println(string(buf[:n]));
    }

    //向文件中写入数据
    for i := 0; i < 5; i++ {
        f3.Write([]byte("写入数据" + strconv.Itoa(i) + "\r\n"));
    }

    //返回一对关联的文件对象
    //从r中可以读取到从w写入的数据
    r, w, _ := os.Pipe();

    //向w中写入字符串
    w.WriteString("写入w");
    buf2 := make([]byte, 128);

    //从r中读取数据
    n, _ := r.Read(buf);
    fmt.Println(string(buf2[:n]));

    //改变工作目录
    os.Mkdir("a", os.ModePerm);
    dir, _ := os.Open("a");

    //改变工作目录到dir,dir必须为一个目录
    dir.Chdir();
    fmt.Println(os.Getwd());

    //读取目录的内容,返回一个FileInfo的slice
    //参数大于0,最多返回n个FileInfo
    //参数小于等于0,返回所有FileInfo
    fi, _ := dir.Readdir(-1);
    for _, v := range fi {
        fmt.Println(v.Name());
    }

    //读取目录中文件对象的名字
    names, _ := dir.Readdirnames(-1);
    fmt.Println(names);

    //获取文件的详细信息,返回FileInfo结构
    fi3, _ := f3.Stat();

    //文件名
    fmt.Println(fi3.Name());

    //文件大小
    fmt.Println(fi3.Size());

    //文件权限
    fmt.Println(fi3.Mode());

    //文件修改时间
    fmt.Println(fi3.ModTime());

    //是否是目录
    fmt.Println(fi3.IsDir());
}

这里os.Open或其他方法,默认路径是work directory,本文后面会做详细解释。

3.os中关于进程的操作
package main

import (
    "os"
    "fmt"
    "time"
)

//os包中关于进程的操作函数

func main() {
    //设置新进程的属性
    attr := &os.ProcAttr{
        //files指定新进程继承的活动文件对象
        //前三个分别为,标准输入、标准输出、标准错误输出
        Files: []*os.File{os.Stdin, os.Stdout, os.Stderr},

        //新进程的环境变量
        Env: os.Environ(),
    }

    //win下通过记事本打开1.txt
    //开始一个新进程
    p, err := os.StartProcess("C:\\Windows\\System32\\notepad.exe", 
    []string{"C:\\Windows\\System32\\notepad.exe", "D:\\1.txt"}, attr);

    if err != nil {
        fmt.Println(err);
    }

    fmt.Println(p);
    fmt.Println("进程ID:", p.Pid);

    //通过进程ID查找进程
    p2, _ := os.FindProcess(p.Pid);
    fmt.Println(p2);

    //等待10秒,执行函数
    time.AfterFunc(time.Second*10, func() {
        //向p进程发送退出信号
        p.Signal(os.Kill);
    });

    //等待进程p的退出,返回进程状态
    ps, _ := p.Wait();
    fmt.Println(ps.String());
}
二、设置配置文件路径
1.os.args

参考Golang 命令行 os.Args 和 flag包\
程序获取运行他时给出的参数,可以通过os包来实现。先看代码:

package main

import (
    "fmt"
    "os"
    "strconv"
)

func main () {
    for idx, args := range os.Args {
        fmt.Println("参数" + strconv.Itoa(idx) + ":", args)
    }
}

运行起来得到的如下:

$go run main.go 1 3 -X ?
参数0: /tmp/go-build116558042/command-line-arguments/_obj/exe/main
参数1: 1
参数2: 3
参数3: -X
参数4: ?

可以看到,命令行参数包括了程序路径本身,以及通常意义上的参数。 程序中os.Args的类型是 []string ,也就是字符串切片。所以可以在for循环的range中遍历,还可以用 len(os.Args) 来获取其数量。

\

image.png

\

如果在goland中设置output directory,就会输出:

参数0: D:\go\TestFile\bin\go_build_TestPath1_go.exe

也就是说,output directory对应着os.Args[0],即go run执行的目录\
PS:这里也能看到下面一个Working directory,对应的正是os.Getwd(),wd即是Working directory缩写,当然可以更改默认值,变成在bin文件夹下:D:\go\TestFile\bin

2.Args[0]为什么不能直接作为程序的绝对路径
curFilename := os.Args[0]
Path, err := exec.LookPath(curFilename)
binaryPath, err = filepath.Abs(Path)
dir := filepath.Dir(binaryPath)

//怎么不直接使用 filepath.Abs(filepath.Dir(os.Args[0]))
//还要通过一层LookPath

假设文件在/home/XXX/a\
args[0]获取的是相对路径,或者说,就是你使用什么命令启动的。\
如果你用./a启动的话,args[0]就是./a,不是绝对路径。\
如果你用./XXX/a启动的话,args[0]就是./XXX/a,不是绝对路径。\
如果用/home/XXX/a启动,那么获取到的就是/home/XXX/a。

argv[0]的做法来自C语言,因此其他语言的argv[0]也就保持了和C语言一致。

获取可执行文件的绝对路径(不包括文件名),请用:

filepath.Abs(filepath.Dir(os.Args[0]))
返回:/home/XXX

补充:获取可执行文件的绝对路径(包括文件名),请用:

filepath.Abs(os.Args[0])
返回:/home/XXX/a
3.LookPath

参考\
golang中os/exec包用法\
Go知识点记录\
Golang学习 - path/filepath 包

func main() {
    f, err := exec.LookPath("ls")
    if err != nil {
        fmt.Println(err)
    }
    fmt.Println(f) //  /bin/ls
}

func LookPath(file string) (string, error) //在环境变量PATH指定的目录中搜索可执行文件,如file中有斜杠,则只在当前目录搜索。返回完整路径或者相对于当前目录的一个相对路径。

package main

import(
        "os"
        "log"
        "os/exec"
        "path"
)

func main() {

        //可执行文件我放在/home/yejianfeng/handcode/test
        //我执行的路径是/home/yejianfeng/

        //获取当前目录
        file, _ := os.Getwd()
        //current path: /home/yejianfeng
        log.Println("current path:", file)

        file, _ := exec.LookPath(os.Args[0])
        //exec path: handcode/test
        log.Println("exec path:", file)

        dir,_ := path.Split(file)
        //exec folder relative path: handcode/
        log.Println("exec folder relative path:", dir)

        //改变当前工作目录
        os.Chdir(dir)
        wd, _ := os.Getwd()
        //exec folder absolute path: /home/yejianfeng/handcode
        log.Println("exec folder absolute path:", wd)
}

Go语言学习之path/filepath包(the way to go)\
4.Abs\
func Abs(path string) (string, error)\
检测地址是否是绝对地址,是绝对地址直接返回,不是绝对地址,会添加当前工作路径到参数path前,然后返回

func TestAbs() {
    fpt, err := filepath.Abs("/hello")
    if err != nil {
        panic(err)
    }
    fmt.Println(fpt)

    fpt, err = filepath.Abs("helleeo")
    if err != nil {
        panic(err)
    }
    fmt.Println(fpt)
}

打印信息:

/hello
/home/xxx/workspace/gotestworkspace/golangtest/helleeo

“/”表示当前路径下

5.golang 正确获取绝对路径的方法

import (
    "fmt"
    "os"
    "os/exec"
    "path/filepath"
    "strings"
)

func main() {
    fmt.Println("GoLang 获取程序运行绝对路径")
    fmt.Println(GetCurrPath())
}

func GetCurrPath() string {
    file, _ := exec.LookPath(os.Args[0])
    path, _ := filepath.Abs(file)
    index := strings.LastIndex(path, string(os.PathSeparator))
    ret := path[:index]
    return ret
}
三、聊一聊,Golang “相对”路径问题
1.传递参数
func main() {
    var appPath string
    flag.StringVar(&appPath, "app-path", "app-path")
    flag.Parse()
    fmt.Printf("App path: %s", appPath)
}
----------
go run main.go --app-path "Your project address"
2.增加os.Getwd()进行多层判断

参见 beego L133-L146 读取 app.conf 的代码,该写法可兼容 go build 和在项目根目录执行 go run ,但是若跨目录执行 go run 就不行

func init() {
    BConfig = newBConfig()
    var err error
    if AppPath, err = filepath.Abs(filepath.Dir(os.Args[0])); err != nil {
        panic(err)
    }
    workPath, err := os.Getwd()
    if err != nil {
        panic(err)
    }
    var filename = "app.conf"
    if os.Getenv("BEEGO_RUNMODE") != "" {
        filename = os.Getenv("BEEGO_RUNMODE") + ".app.conf"
    }
    appConfigPath = filepath.Join(workPath, "conf", filename)
    if !utils.FileExists(appConfigPath) {
        appConfigPath = filepath.Join(AppPath, "conf", filename)
        if !utils.FileExists(appConfigPath) {
            AppConfig = &beegoAppConfig{innerConfig: config.NewFakeConfig()}
            return
        }
    }
    if err = parseConfig(appConfigPath); err != nil {
        panic(err)
    }
}

这里主要目的就是找到appConfigPath。如果忽略BEEGO_RUNMODE,可以当作filename="app.conf",首先会直接使用workPath进行拼接:workPath, err := os.Getwd(),相当于运行路径下比如D:\go\TestFile,拼接完成后就是D:\go\TestFile\conf\app.conf,然后使用utils.FileExists判断这个文件是否存在。如果不存在,会用AppPath拼接。

这里AppPath是D:\go\TestFile\bin,这是因为我们的os.Args[0]=D:\go\TestFile\bin\go_build_TestPath1_go.exe,filepath.Dir会返回路径最后一个元素的目录。如果对照goland上面的设置,可以看出workPath对应的是Working directory,AppPath对应的是output directory。

3.配置全局系统变量

参见 gogs L351 读取GOGS_WORK_DIR进行拼接的代码

// execPath returns the executable path.
func execPath() (string, error) {
    file, err := exec.LookPath(os.Args[0])
    if err != nil {
        return "", err
    }
    return filepath.Abs(file)
}

func init() {
    IsWindows = runtime.GOOS == "windows"
    log.New(log.CONSOLE, log.ConsoleConfig{})

    var err error
    if AppPath, err = execPath(); err != nil {
        log.Fatal(2, "Fail to get app path: %v\n", err)
    }

    // Note: we don't use path.Dir here because it does not handle case
    //  which path starts with two "/" in Windows: "//psf/Home/..."
    AppPath = strings.Replace(AppPath, "\\", "/", -1)
}

// WorkDir returns absolute path of work directory.
func WorkDir() (string, error) {
    wd := os.Getenv("GOGS_WORK_DIR")
    if len(wd) > 0 {
        return wd, nil
    }

    i := strings.LastIndex(AppPath, "/")
    if i == -1 {
        return AppPath, nil
    }
    return AppPath[:i], nil
}

// NewContext initializes configuration context.
// NOTE: do not print any log except error.
func NewContext() {
    workDir, err := WorkDir()
    if err != nil {
        log.Fatal(2, "Fail to get work directory: %v", err)
    }

    Cfg, err = ini.LoadSources(ini.LoadOptions{
        IgnoreInlineComment: true,
    }, bindata.MustAsset("conf/app.ini"))
    if err != nil {
        log.Fatal(2, "Fail to parse 'conf/app.ini': %v", err)
    }

    CustomPath = os.Getenv("GOGS_CUSTOM")
    if len(CustomPath) == 0 {
        CustomPath = workDir + "/custom"
    }

    if len(CustomConf) == 0 {
        CustomConf = CustomPath + "/conf/app.ini"
    }
...
4.利用系统自带变量

简单来说就是通过系统自带的全局变量,例如$HOME等,将配置文件存放在$HOME/conf或/etc/conf下。这样子就能更加固定的存放配置文件,不需要额外去设置一个环境变量(这点今早与一位SFer讨论了一波,感谢)。

作者:合肥懒皮
链接:https://www.jianshu.com/p/cec7cc3b3d98\
来源:简书

本作品采用《CC 协议》,转载必须注明作者和本文链接
讨论数量: 1

看着都疯了,每行都去加上个莫名其妙的分号

3年前 评论

讨论应以学习和精进为目的。请勿发布不友善或者负能量的内容,与人为善,比聪明更重要!