Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

获取当前工作目录并保存到一个包级别的变量中 #22

Open
AlexZ33 opened this issue Sep 28, 2022 · 0 comments
Open

获取当前工作目录并保存到一个包级别的变量中 #22

AlexZ33 opened this issue Sep 28, 2022 · 0 comments

Comments

@AlexZ33
Copy link
Member

AlexZ33 commented Sep 28, 2022

可以通过os.Getwd完成
但是当我们需要处理错误时,将这个从主逻辑中分离出来会更好。函数log.Fatalf用于打印日志信息然后调用os.Exit()来终止程序。

var cwd string

func init(){
    var err error
    cwd, err = os.Getwd()
    if err != nil{
        log.Fatalf("os.Getwd failed: %v", err)
    }
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant